1、安装MySQL的库
这里推荐使用mymysql
https://github.com/ziutek/mymysql
WIN安装方法:
在Cmd下执行 3句
go get github.com/ziutek/mymysql/thrsafe
go get github.com/ziutek/mymysql/autorc
go get github.com/ziutek/mymysql/godrv
2、使用方法
import(
"fmt"
"github.com/ziutek/mymysql/mysql"
_ "github.com/ziutek/mymysql/native"
)
func main() {
db := mysql.New("tcp", "", "连接地址","数据库用户","密码","数据库名"
err := db.Connect()
if err != nil{
fmt.Println(err); //连接失败
}
rows, res, err := db.Query("select * from 表")
if err == nil{
fmt.Println(rows[0].Str(0)) //字符串显示第一个字段内容
name := res.Map("字段名") //查询字段名返回序号
fmt.Println(rows[0].Str(name))
}
}