ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
>[info] go操作mysql [TOC] ## 1. Mysql使用 使用第三方开源的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 ``` 1.1 新建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=2 DEFAULT CHARSET=utf8; CREATE TABLE `place` ( `country` varchar(200) DEFAULT NULL, `city` varchar(200) DEFAULT NULL, `telcode` int(11) DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8; ``` 1.2 连接mysql ``` // database, err := sqlx.Open("数据库类型", "用户名:密码@tcp(地址:端口)/数据库名") database, err := sqlx.Open("mysql", "root:XXXX@tcp(127.0.0.1:3306)/test") ``` ## 2. Insert操作 * **示例:** ~~~ package main import ( "fmt" _ "github.com/go-sql-driver/mysql" "github.com/jmoiron/sqlx" ) var Db *sqlx.DB type Person struct { UserId int `db:"user_id"` Username string `db:"username"` Sex string `db:"sex"` Email string `db:"email"` } func init() { database, err := sqlx.Open("mysql", "root:root@tcp(127.0.0.1:3306)/test") if err != nil { fmt.Println("open mysql failed,", err) return } Db = database } 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) } ~~~ * **结果:** ``` insert succ: 2 ``` ## 3. Select操作 * **示例:** ~~~ package main import ( "fmt" _ "github.com/go-sql-driver/mysql" "github.com/jmoiron/sqlx" ) var Db *sqlx.DB type Person struct { UserId int `db:"user_id"` Username string `db:"username"` Sex string `db:"sex"` Email string `db:"email"` } func init() { database, err := sqlx.Open("mysql", "root:root@tcp(127.0.0.1:3306)/test") if err != nil { fmt.Println("open mysql failed,", err) return } Db = database } func main() { var person []Person err := Db.Select(&person, "select user_id, username, sex, email from person where user_id =?", 2) if err != nil { fmt.Println("exec failed, ", err) return } fmt.Println("select succ:", person) } ~~~ * **结果:** ``` select succ: [{2 stu001 man stu01@qq.com}] ``` ## 4. Update操作 * **示例:** ~~~ package main import ( "fmt" _ "github.com/go-sql-driver/mysql" "github.com/jmoiron/sqlx" ) var Db *sqlx.DB type Person struct { UserId int `db:"user_id"` Username string `db:"username"` Sex string `db:"sex"` Email string `db:"email"` } func init() { database, err := sqlx.Open("mysql", "root:root@tcp(127.0.0.1:3306)/test") if err != nil { fmt.Println("open mysql failed,", err) return } Db = database } func main() { res, err := Db.Exec("update person set username=? where user_id=?", "stu0003", 2) 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 ``` ## 5. Delete操作 * **示例:** ~~~ package main import ( "fmt" _ "github.com/go-sql-driver/mysql" "github.com/jmoiron/sqlx" ) var Db *sqlx.DB type Person struct { UserId int `db:"user_id"` Username string `db:"username"` Sex string `db:"sex"` Email string `db:"email"` } func init() { database, err := sqlx.Open("mysql", "root:root@tcp(127.0.0.1:3306)/test") if err != nil { fmt.Println("open mysql failed,", err) return } Db = database } func main() { res, err := Db.Exec("delete from person where user_id=?", 2) 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 ``` ## 6. 事务操作 mysql事务特性: ``` 1. 原子性 2. 一致性 3. 隔离性 4. 持久性 ``` * **示例:** ~~~ package main import ( "fmt" _ "github.com/go-sql-driver/mysql" "github.com/jmoiron/sqlx" ) var Db *sqlx.DB type Person struct { UserId int `db:"user_id"` Username string `db:"username"` Sex string `db:"sex"` Email string `db:"email"` } func init() { database, err := sqlx.Open("mysql", "root: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 := Db.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 } conn.Commit() fmt.Println("insert succ:", id) } ~~~ * **结果:** ``` insert succ: 3 ```