mysql
1、mysql编程
a. 安装sqlx
go get github.com/jmoiron/sqlx
b. 链接mysql
database, err := sqlx.Open("mysql", "root:@tcp(127.0.0.1:3306)/test")
2、mysql相关操作
insert操作 r, err := Db.Exec("insert into person(username, sex, email)values(?, ?, ?)", "stu001", "man", "stu01@qq.com") Select 操作 err := Db.Select(&person, "select user_id, username, sex, email from person where user_id=?", 1) update操作 _, err := Db.Exec("update person set username=? where user_id=?", "stu0001", 1) Delete 操作 _, err := Db.Exec("delete from person where user_id=?", 1)
插入:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
import ( "fmt" "time" _ "github.com/go-sql-driver/mysql" //mysql驱动实例注册,在init注册到go的接口里面去 "github.com/jmoiron/sqlx" "math/rand" ) type UserInfo struct { UserId int `db:"user_id"` Username string `db:"username"` Sex string `db:"sex"` Email string `db:"email"` } func main() { Db, err := sqlx.Open("mysql", "root:@tcp(10.0.0.200:3306)/godb") if err != nil { fmt.Println("connect to msyql failed, ", err) return } startTime := time.Now().UnixNano() fmt.Println("connect to mysql succ") username := fmt.Sprintf("user%d", rand.Int63()) email := fmt.Sprintf("%d@qq.com", rand.Int()) sex := "男" //插入数据 result, err := Db.Exec("insert into user_info(username, sex, email)values(?,?,?)", username, sex, email) if err != nil { fmt.Println("insert failed, err:", err) return } user_id, _ := result.LastInsertId() //返回插入的行的id fmt.Println("insert succ, user_id:", user_id) endTime := time.Now().UnixNano() fmt.Printf("insert succ cost:%d ms\n", (endTime-startTime)/1000/1000) Db.Close() }
查询:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package main import ( "fmt" "time" _ "github.com/go-sql-driver/mysql" //mysql驱动实例注册,在init注册到go的接口里面去 "github.com/jmoiron/sqlx" ) type UserInfo struct { UserId int `db:"user_id"` Username string `db:"username"` Sex string `db:"sex"` Email string `db:"email"` } func main() { Db, err := sqlx.Open("mysql", "root:@tcp(10.0.0.200:3306)/godb") if err != nil { fmt.Println("connect to msyql failed, ", err) return } startTime := time.Now().UnixNano() var userInfo UserInfo //查询一条语句 err = Db.Get(&userInfo, "select user_id, username, sex, email from user_info where username=?", "user1674879938132494608") if err != nil { fmt.Println("get failed, err:", err) return } fmt.Printf("userInfo:%#v\n", userInfo) endTime := time.Now().UnixNano() fmt.Printf("insert succ cost:%d ms\n", (endTime-startTime)/1000/1000) var userInfoList []*UserInfo //查询多条语句 err = Db.Select(&userInfoList, "select user_id, username, sex, email from user_info where user_id>?", 2) fmt.Printf("user_info_list:%#v\n", userInfoList) if err != nil { fmt.Println("select failed, err:", err) return } Db.Close() }
数据库的原子操作:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package main import ( "fmt" "time" _ "github.com/go-sql-driver/mysql" "github.com/jmoiron/sqlx" ) type UserInfo struct { UserId int `db:"user_id"` Username string `db:"username"` Sex string `db:"sex"` Email string `db:"email"` } func main() { Db, err := sqlx.Open("mysql", "root:@tcp(10.0.0.200:3306)/godb") if err != nil { fmt.Println("connect to msyql failed, ", err) return } startTime := time.Now().UnixNano() conn, _ := Db.Begin() //原子操作 _, err = conn.Exec("insert into user_info(username, sex, email)values(?,?,?)", "user01", "man", "email") if err != nil { conn.Rollback() //回滚 fmt.Println("insert failed, err:", err) return } _, err = conn.Exec("insert into user_info(username, sex, email)values(?,?,?)", "user01", "man", "email") if err != nil { conn.Rollback() //回滚 fmt.Println("insert failed, err:", err) return } conn.Commit() Db.Close() }