Golang操作MySQL
Posted 知其黑、受其白
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Golang操作MySQL相关的知识,希望对你有一定的参考价值。
阅读目录
mysql 使用
新建 test 数据库,person、place 表。
CREATE TABLE `person` (
`user_id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(260) DEFAULT NULL,
`sex` varchar(260) DEFAULT NULL,
`email` varchar(260) DEFAULT NULL,
PRIMARY KEY (`user_id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
CREATE TABLE place (
country varchar(200),
city varchar(200),
telcode int
)ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
使用第三方开源的 mysql 库: github.com/go-sql-driver/mysql (mysql驱动) github.com/jmoiron/sqlx (基于mysql驱动的封装)
命令行输入 :
go get github.com/go-sql-driver/mysql
go get github.com/jmoiron/sqlx
链接 mysql
database, err := sqlx.Open("mysql", "root:XXXX@tcp(127.0.0.1:3306)/test")
//database, err := sqlx.Open("数据库类型", "用户名:密码@tcp(地址:端口)/数据库名")
Insert 操作
package main
import (
"fmt"
_ "github.com/go-sql-driver/mysql"
"github.com/jmoiron/sqlx"
)
type Person struct
UserId int `db:"user_id"`
Username string `db:"username"`
Sex string `db:"sex"`
Email string `db:"email"`
type Place struct
Country string `db:"country"`
City string `db:"city"`
TelCode int `db:"telcode"`
var Db *sqlx.DB
func init()
database, err := sqlx.Open("mysql", "root:@tcp(127.0.0.1:3306)/test")
if err != nil
fmt.Println("open mysql failed,", err)
return
Db = database
// defer Db.Close() // 注意这行代码要写在上面err判断的下面
func main()
r, err := Db.Exec("insert into person(username, sex, email)values(?, ?, ?)", "stu001", "man", "stu01@qq.com")
if err != nil
fmt.Println("exec failed, ", err)
return
id, err := r.LastInsertId()
if err != nil
fmt.Println("exec failed, ", err)
return
fmt.Println("insert succ:", id)
Select 操作
package main
import (
"fmt"
_ "github.com/go-sql-driver/mysql"
"github.com/jmoiron/sqlx"
)
type Person struct
UserId int `db:"user_id"`
Username string `db:"username"`
Sex string `db:"sex"`
Email string `db:"email"`
type Place struct
Country string `db:"country"`
City string `db:"city"`
TelCode int `db:"telcode"`
var Db *sqlx.DB
func init()
database, err := sqlx.Open("mysql", "root:@tcp(127.0.0.1:3306)/test")
if err != nil
fmt.Println("open mysql failed,", err)
return
Db = database
// defer Db.Close() // 注意这行代码要写在上面err判断的下面
func main()
var person []Person
err := Db.Select(&person, "select user_id, username, sex, email from person where user_id=?", 1)
if err != nil
fmt.Println("exec failed, ", err)
return
fmt.Println("select succ:", person)
输出结果:
select succ: [1 stu001 man stu01@qq.com]
Update 操作
package main
import (
"fmt"
_ "github.com/go-sql-driver/mysql"
"github.com/jmoiron/sqlx"
)
type Person struct
UserId int `db:"user_id"`
Username string `db:"username"`
Sex string `db:"sex"`
Email string `db:"email"`
type Place struct
Country string `db:"country"`
City string `db:"city"`
TelCode int `db:"telcode"`
var Db *sqlx.DB
func init()
database, err := sqlx.Open("mysql", "root:@tcp(127.0.0.1:3306)/test")
if err != nil
fmt.Println("open mysql failed,", err)
return
Db = database
// defer Db.Close() // 注意这行代码要写在上面err判断的下面
func main()
res, err := Db.Exec("update person set username=? where user_id=?", "stu0003", 1)
if err != nil
fmt.Println("exec failed, ", err)
return
row, err := res.RowsAffected()
if err != nil
fmt.Println("rows failed, ", err)
fmt.Println("update succ:", row)
运行结果
第一次运行:
update succ: 1
# 受影响的行数 1
第二次运行
update succ: 0
# 受影响的行数 0
Delete 操作
package main
import (
"fmt"
_ "github.com/go-sql-driver/mysql"
"github.com/jmoiron/sqlx"
)
type Person struct
UserId int `db:"user_id"`
Username string `db:"username"`
Sex string `db:"sex"`
Email string `db:"email"`
type Place struct
Country string `db:"country"`
City string `db:"city"`
TelCode int `db:"telcode"`
var Db *sqlx.DB
func init()
database, err := sqlx.Open("mysql", "root:@tcp(127.0.0.1:3306)/test")
if err != nil
fmt.Println("open mysql failed,", err)
return
Db = database
// defer Db.Close() // 注意这行代码要写在上面err判断的下面
func main()
res, err := Db.Exec("delete from person where user_id=?", 1)
if err != nil
fmt.Println("exec failed, ", err)
return
row, err := res.RowsAffected()
if err != nil
fmt.Println("rows failed, ", err)
fmt.Println("delete succ: ", row)
运行结果
第一次运行:
delete succ: 1
# 受影响的行数 1
第二次运行:
delete succ: 0
# 受影响的行数 0
MySQL 事务
mysql事务特性:
1) 原子性
2) 一致性
3) 隔离性
4) 持久性
golang MySQL事务应用:
1) import (“github.com/jmoiron/sqlx")
2) Db.Begin() 开始事务
3) Db.Commit() 提交事务
4) Db.Rollback() 回滚事务
package main
import (
"fmt"
_ "github.com/go-sql-driver/mysql"
"github.com/jmoiron/sqlx"
)
type Person struct
UserId int `db:"user_id"`
Username string `db:"username"`
Sex string `db:"sex"`
Email string `db:"email"`
type Place struct
Country string `db:"country"`
City string `db:"city"`
TelCode int `db:"telcode"`
var Db *sqlx.DB
func init()
database, err := sqlx.Open("mysql", "root:@tcp(127.0.0.1:3306)/test")
if err != nil
fmt.Println("open mysql failed,", err)
return
Db = database
func main()
conn, err := Db.Begin()
if err != nil
fmt.Println("begin failed :", err)
return
r, err := conn.Exec("insert into person(username, sex, email)values(?, ?, ?)", "stu001", "man", "stu01@qq.com")
if err != nil
fmt.Println("exec failed, ", err)
conn.Rollback()
return
id, err := r.LastInsertId()
if err != nil
fmt.Println("exec failed, ", err)
conn.Rollback()
return
fmt.Println("insert succ:", id)
r, err = conn.Exec("insert into person(username, sex, email)values(?, ?, ?)", "stu001", "man", "stu01@qq.com")
if err != nil
fmt.Println("exec failed, ", err)
conn.Rollback()
return
id, err = r.LastInsertId()
if err != nil
fmt.Println("exec failed, ", err)
conn.Rollback()
return
fmt.Println("insert succ:", id)
conn.Commit()
输出结果:
PS E:\\go_test> go run .\\main.go
insert succ: 2
insert succ: 3
PS E:\\go_test>
以上是关于Golang操作MySQL的主要内容,如果未能解决你的问题,请参考以下文章