I am trying to connect to a mysql database I have locally, using golang, it builds just fine but running it gives me the following error:
panic: sql: expected 0 arguments, got 1
My connection looks like this:
package main
import (
"database/sql"
"fmt"
_ "github.com/go-sql-driver/mysql"
)
func DBConn() {
team := "software"
db, err := sql.Open("mysql", "root:12345678@tcp(localhost:3306)/flexlocal")
if err != nil {
fmt.Println("this is where it all went wrong")
fmt.Printf(err.Error())
panic(err)
}
}
According to my research this is how it works however it just isn't working for me.
Please try dbconn function to connect with mysql in go
package main
import (
"database/sql"
"fmt"
_ "github.com/go-sql-driver/mysql"
)
func dbConn(setDatbaseInstance string) (db *sql.DB) {
dbDriver := "mysql"
dbUser := "*****"
dbPass := "*****"
dbName := "*****"
db, err := sql.Open(dbDriver, dbUser+":"+dbPass+"@tcp("+setDatbaseInstance+":3306)/"+dbName)
if err != nil {
fmt.Printf("%#v\n DB_ERROR_CONNECTION\n", err.Error());
// return err.Error()
}else{
fmt.Println("Connection Established")
}
erro:=db.Ping()
if erro!=nil {
//do something here
fmt.Printf("%#v\n DB_PING_ERROR_CONNECTION\n", erro.Error());
}
return db
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.