go数据库操作
下载数据库驱动
比如要使用MYSQL,就要下载MYSQL驱动:go get github.com/go-sql-driver/mysql
连接数据库
连接数据的DSN格式为: username:password@protocol(address)/dbname?param=value
db, err := sql.Open("mysql", "root:root@tcp(127.0.0.1:3306)/test?charset=utf8") if err != nil { fmt.Println("failed to open database:", err.Error()) return } defer db.Close()
返回的DB对象,实际封装了一个数据库连接池,对于goroutine是线程安全的,可以放心使用。这个数据库连接池由"database/sql"包负责自动创建和回收。连接池的大小可以由SetMaxIdleConns指定。
需要注意的是,创建DB对象成功,并不代表已经成功的连接了数据库,数据库连接只有在真正需要的时候才会被创建。
关闭数据库
defer db.Close()
CRUD
DB中执行SQL通过Exec和Query方法,查询操作是通过Query完成,它会返回一个sql.Rows的结果集,包含一个游标用来遍历查询结果;Exec方法返回的是sql.Result对象,用于检测操作结果,及被影响记录数。
查询
rows, err := db.Query("SELECT * FROM user") if err != nil { fmt.Println("fetech data failed:", err.Error()) return } defer rows.Close() for rows.Next() { var uid int var name, password string rows.Scan(&uid, &name, &password) fmt.Println("uid:", uid, "name:", name, "password:", password)
新增
result, err := db.Exec("INSERT INTO user(name,password) VALUES('tom', 'tom')") if err != nil { fmt.Println("insert data failed:", err.Error()) return } id, err := result.LastInsertId() if err != nil { fmt.Println("fetch last insert id failed:", err.Error()) return } fmt.Println("insert new record", id)
修改
result, err = db.Exec("UPDATE user SET password=? WHERE name=?", "tom_new_password", "tom") if err != nil { fmt.Println("update data failed:", err.Error()) return } num, err := result.RowsAffected() if err != nil { fmt.Println("fetch row affected failed:", err.Error()) return } fmt.Println("update recors number", num)
删除
result, err = db.Exec("DELETE FROM user WHERE name=?", "tom") if err != nil { fmt.Println("delete data failed:", err.Error()) return } num, err = result.RowsAffected() if err != nil { fmt.Println("fetch row affected failed:", err.Error()) return } fmt.Println("delete record number", num)
事务控制
sql.Tx用来支持事务处理
tx, err := db.Begin() result, err = tx.Exec("DELETE FROM order WHERE uid=? ", 2) if err != nil { fmt.Println("delete data failed:", err.Error()) return } num, err = result.RowsAffected() if err != nil { fmt.Println("fetch row affected failed:", err.Error()) return } fmt.Println("delete record number", num) result, err = tx.Exec("DELETE FROM user WHERE uid=? ", 2) if err != nil { fmt.Println("delete data failed:", err.Error()) return } num, err = result.RowsAffected() if err != nil { fmt.Println("fetch row affected failed:", err.Error()) return } fmt.Println("delete record number", num) // 根据条件回滚或者提交 // tx.Rollback() tx.Commit()
预备表达式
sql.Stmt支持预备表达式,可以用来优化SQL查询提高性能,减少SQL注入的风险, DB.Prepare()和Tx.Prepare()都提供了对于预备表达式的支持。
stmt, err := db.Prepare("DELETE FROM order WHERE oid=?") if err != nil { fmt.Println("fetch row affected failed:", err.Error()) return } result, err = stmt.Exec(1) if err != nil { fmt.Println("delete data failed:", err.Error()) return } num, err = result.RowsAffected() if err != nil { fmt.Println("fetch row affected failed:", err.Error()) return } fmt.Println("delete record number", num)
每次db.Query()操作后,都建议调用rows.Close()