With that out of the way, you are ready to connect. Create a main.go file and insert the following code: package main import ( "fmt" "database/sql" _ "github.com/go-sql-driver/mysql" ) func main() { db, err := sql. Open("mysql", "root:<yourMySQLdatabasepassword>@tcp(127.0.
To add a connection, click the [+] icon to the right of the MySQL Connections title on the home screen. This opens the Setup New Connection form, as the following figure shows. The Configure Server Management button (bottom left) opens an optional configuration wizard for setting shell commands on the host.
This connection method enables MySQL Workbench to connect to MySQL Server using TCP/IP. In addition to naming your new connection in the field provided, you can select from the following tabs to specify connection values: Parameters, SSL, and Advanced.
A few drivers are available but you should only consider those that implement the database/sql API as
Two fast and reliable drivers are available for MySQL :
I've used both of them in production, programs are running for months with connection numbers in the millions without failure.
Other SQL database drivers are listed on go-wiki.
Import when using MyMySQL :
import (
"database/sql"
_ "github.com/ziutek/mymysql/godrv"
)
Import when using Go-MySQL-Driver :
import (
"database/sql"
_ "github.com/go-sql-driver/mysql"
)
Connecting and closing using MyMySQL :
con, err := sql.Open("mymysql", database+"/"+user+"/"+password)
defer con.Close()
// here you can use the connection, it will be closed when function returns
Connecting and closing using Go-MySQL-Driver :
con, err := sql.Open("mysql", store.user+":"+store.password+"@/"+store.database)
defer con.Close()
Select one row :
row := con.QueryRow("select mdpr, x, y, z from sometable where id=?", id)
cb := new(SomeThing)
err := row.Scan(&cb.Mdpr, &cb.X, &cb.Y, &cb.Z)
Select multiple rows and build an array with results :
rows, err := con.Query("select a, b from item where p1=? and p2=?", p1, p2)
if err != nil { /* error handling */}
items := make([]*SomeStruct, 0, 10)
var ida, idb uint
for rows.Next() {
err = rows.Scan(&ida, &idb)
if err != nil { /* error handling */}
items = append(items, &SomeStruct{ida, idb})
}
Insert :
_, err = con.Exec("insert into tbl (id, mdpr, isok) values (?, ?, 1)", id, mdpr)
You'll see that working in Go with MySQL is a delightful experience : I never had a problem, my servers run for months without errors or leaks. The fact that most functions simply take a variable number of arguments lighten a task which is tedious in many languages.
Note that if, in the future, you need to use another MySQL driver, you'll just have to change two lines in one go file : the line doing the import and the line opening the connection.
few things to take note the select 1 row example :
row := con.QueryRow("select mdpr, x, y, z from sometable where id=?",id)
cb := new(SomeThing)
err := row.Scan(&cb.Mdpr, &cb.X, &cb.Y, &cb.Z)
there is a missing row.Next()
in this example. it need to call the row.Next()
to grab the first row returned.
also there is some inflexibility to the library which in some way try to promote data minimalism. if you try to select columns that is not Scan it will throw errors (not just warnings)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With