从 Go 连接到 MySQL 的推荐方法是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11353679/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What's the recommended way to connect to MySQL from Go?
提问by Sergi Mansilla
I am looking for a reliable solution to connect to a MySQL database from Go. I've seen some libraries around but it is difficult to determine the different states of completeness and current maintenance. I don't have complicated needs, but I'd like to know what people are relying on, or what's the most standard solution to connect to MySQL.
我正在寻找一个可靠的解决方案来从 Go 连接到 MySQL 数据库。我已经看到了一些库,但很难确定完整性和当前维护的不同状态。我没有复杂的需求,但我想知道人们依赖什么,或者连接到 MySQL 的最标准的解决方案是什么。
回答by Denys Séguret
A few drivers are available but you should only consider those that implement the database/sqlAPI as
有一些驱动程序可用,但您应该只考虑那些实现数据库/sqlAPI 的驱动程序
- it provides a clean and efficient syntax,
- it ensures you can later change the driver without changing your code, apart the import and connection.
- 它提供了一个干净有效的语法,
- 它确保您可以在不更改代码的情况下稍后更改驱动程序,除了导入和连接。
Two fast and reliable drivers are available for MySQL :
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.
go-wiki 上列出了其他 SQL 数据库驱动程序。
Import when using MyMySQL :
使用 MyMySQL 时导入:
import (
"database/sql"
_ "github.com/ziutek/mymysql/godrv"
)
Import when using Go-MySQL-Driver :
使用 Go-MySQL-Driver 时导入:
import (
"database/sql"
_ "github.com/go-sql-driver/mysql"
)
Connecting and closing using MyMySQL :
使用 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 :
使用 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 neverhad 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.
你会发现在 Go 中使用 MySQL 是一种愉快的体验:我从来没有遇到过问题,我的服务器运行了几个月没有错误或泄漏。大多数函数只采用可变数量的参数这一事实减轻了许多语言中乏味的任务。
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.
请注意,如果将来您需要使用另一个 MySQL 驱动程序,您只需更改一个 go 文件中的两行:执行导入的行和打开连接的行。
回答by Badoet
few things to take note the select 1 row example :
有几件事要注意选择 1 行示例:
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.
row.Next()
在这个例子中有一个缺失。它需要调用row.Next()
来获取返回的第一行。
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)
图书馆也有一些不灵活的地方,它在某种程度上试图促进数据极简主义。如果您尝试选择不是 Scan 的列,它将抛出错误(不仅仅是警告)