參考資料
鏈接
mysql
name:password@tcp(ip:3306)/dbname?charset=utf8&loc=Local
ipv6:mysql
user:password@tcp([hostname]:port)/dbname
SQLInterface
Introduction
The database/sql package provides a generic interface around SQL (or SQL-like) databases. See the official documentation for details.github
This page provides example usage patterns.golang
Database driver
The database/sql package must be used in conjunction with a database driver. See http://golang.org/s/sqldrivers for a list of drivers.sql
The documentation below assumes a driver has been imported.數據庫
Connecting to a database
Open is used to create a database handle:緩存
db, err := sql.Open(driver, dataSourceName)
Where driver specifies a database driver and dataSourceName specifies database-specific connection information such as database name and authentication credentials.tcp
Note that Open does not directly open a database connection: this is deferred until a query is made. To verify that a connection can be made before making a query, use the Ping function:ide
if err := db.Ping(); err != nil { log.Fatal(err) }
After use, the database is closed using Close.函數
Executing queries
Exec is used for queries where no rows are returned:
result, err := db.Exec( "INSERT INTO users (name, age) VALUES ($1, $2)", "gopher", 27, )
Where result contains the last insert ID and number of rows affected. The availability of these values is dependent on the database driver.
Query is used for retrieval:
rows, err := db.Query("SELECT name FROM users WHERE age = $1", age) if err != nil { log.Fatal(err) } for rows.Next() { var name string if err := rows.Scan(&name); err != nil { log.Fatal(err) } fmt.Printf("%s is %d\n", name, age) } if err := rows.Err(); err != nil { log.Fatal(err) }
QueryRow is used where only a single row is expected:
var age int64 row := db.QueryRow("SELECT age FROM users WHERE name = $1", name) err := row.Scan(&age)
Prepared statements can be created with Prepare:
age := 27 stmt, err := db.Prepare("SELECT name FROM users WHERE age = $1") if err != nil { log.Fatal(err) } rows, err := stmt.Query(age) // process rows
Exec, Query and QueryRow can be called on statements. After use, a statement should be closed with Close.
Transactions
Transactions are started with Begin:
tx, err := db.Begin() if err != nil { log.Fatal(err) }
The Exec, Query, QueryRow and Prepare functions already covered can be used in a transaction.
A transaction must end with a call to Commit or Rollback.
Dealing with NULL
If a database column is nullable, one of the types supporting null values should be passed to Scan.
For example, if the name column in the names table is nullable:
var name NullString err := db.QueryRow("SELECT name FROM names WHERE id = $1", id).Scan(&name) ... if name.Valid { // use name.String } else { // value is NULL }
Only NullBool, NullFloat64, NullInt64 and NullString are implemented in database/sql. Implementations of database-specific null types are left to the database driver.
批量操做各類方式效率分析
package main import ( "strconv" "database/sql" _ "github.com/go-sql-driver/mysql" "fmt" "time" "log" ) var db = &sql.DB{} func init(){ db,_ = sql.Open("mysql", "root:root@/book") } func main() { insert() query() update() query() delete() } func update(){ //方式1 update start := time.Now() for i := 1001;i<=1100;i++{ db.Exec("UPdate user set age=? where uid=? ",i,i) } end := time.Now() fmt.Println("方式1 update total time:",end.Sub(start).Seconds()) //方式2 update start = time.Now() for i := 1101;i<=1200;i++{ stm,_ := db.Prepare("UPdate user set age=? where uid=? ") stm.Exec(i,i) stm.Close() } end = time.Now() fmt.Println("方式2 update total time:",end.Sub(start).Seconds()) //方式3 update start = time.Now() stm,_ := db.Prepare("UPdate user set age=? where uid=?") for i := 1201;i<=1300;i++{ stm.Exec(i,i) } stm.Close() end = time.Now() fmt.Println("方式3 update total time:",end.Sub(start).Seconds()) //方式4 update start = time.Now() tx,_ := db.Begin() for i := 1301;i<=1400;i++{ tx.Exec("UPdate user set age=? where uid=?",i,i) } tx.Commit() end = time.Now() fmt.Println("方式4 update total time:",end.Sub(start).Seconds()) //方式5 update start = time.Now() for i := 1401;i<=1500;i++{ tx,_ := db.Begin() tx.Exec("UPdate user set age=? where uid=?",i,i) tx.Commit() } end = time.Now() fmt.Println("方式5 update total time:",end.Sub(start).Seconds()) // 方式1 update total time: 7.3394198 // 方式2 update total time: 7.8464488 // 方式3 update total time: 6.0053435 // 方式4 update total time: 0.6630379000000001 // 方式5 update total time: 4.5402597 } func delete(){ //方式1 delete start := time.Now() for i := 1001;i<=1100;i++{ db.Exec("DELETE FROM USER WHERE uid=?",i) } end := time.Now() fmt.Println("方式1 delete total time:",end.Sub(start).Seconds()) //方式2 delete start = time.Now() for i := 1101;i<=1200;i++{ stm,_ := db.Prepare("DELETE FROM USER WHERE uid=?") stm.Exec(i) stm.Close() } end = time.Now() fmt.Println("方式2 delete total time:",end.Sub(start).Seconds()) //方式3 delete start = time.Now() stm,_ := db.Prepare("DELETE FROM USER WHERE uid=?") for i := 1201;i<=1300;i++{ stm.Exec(i) } stm.Close() end = time.Now() fmt.Println("方式3 delete total time:",end.Sub(start).Seconds()) //方式4 delete start = time.Now() tx,_ := db.Begin() for i := 1301;i<=1400;i++{ tx.Exec("DELETE FROM USER WHERE uid=?",i) } tx.Commit() end = time.Now() fmt.Println("方式4 delete total time:",end.Sub(start).Seconds()) //方式5 delete start = time.Now() for i := 1401;i<=1500;i++{ tx,_ := db.Begin() tx.Exec("DELETE FROM USER WHERE uid=?",i) tx.Commit() } end = time.Now() fmt.Println("方式5 delete total time:",end.Sub(start).Seconds()) // 方式1 delete total time: 3.8652211000000003 // 方式2 delete total time: 3.8582207 // 方式3 delete total time: 3.6972114 // 方式4 delete total time: 0.43202470000000004 // 方式5 delete total time: 3.7972172 } func query(){ //方式1 query start := time.Now() rows,_ := db.Query("SELECT uid,username FROM USER") defer rows.Close() for rows.Next(){ var name string var id int if err := rows.Scan(&id,&name); err != nil { log.Fatal(err) } //fmt.Printf("name:%s ,id:is %d\n", name, id) } end := time.Now() fmt.Println("方式1 query total time:",end.Sub(start).Seconds()) //方式2 query start = time.Now() stm,_ := db.Prepare("SELECT uid,username FROM USER") defer stm.Close() rows,_ = stm.Query() defer rows.Close() for rows.Next(){ var name string var id int if err := rows.Scan(&id,&name); err != nil { log.Fatal(err) } // fmt.Printf("name:%s ,id:is %d\n", name, id) } end = time.Now() fmt.Println("方式2 query total time:",end.Sub(start).Seconds()) //方式3 query start = time.Now() tx,_ := db.Begin() defer tx.Commit() rows,_ = tx.Query("SELECT uid,username FROM USER") defer rows.Close() for rows.Next(){ var name string var id int if err := rows.Scan(&id,&name); err != nil { log.Fatal(err) } //fmt.Printf("name:%s ,id:is %d\n", name, id) } end = time.Now() fmt.Println("方式3 query total time:",end.Sub(start).Seconds()) // 方式1 query total time: 0.0070004 // 方式2 query total time: 0.0100006 // 方式3 query total time: 0.0100006 } func insert() { //方式1 insert //strconv,int轉string:strconv.Itoa(i) start := time.Now() for i := 1001;i<=1100;i++{ //每次循環內部都會去鏈接池獲取一個新的鏈接,效率低下 db.Exec("INSERT INTO user(uid,username,age) values(?,?,?)",i,"user"+strconv.Itoa(i),i-1000) } end := time.Now() fmt.Println("方式1 insert total time:",end.Sub(start).Seconds()) //方式2 insert start = time.Now() for i := 1101;i<=1200;i++{ //Prepare函數每次循環內部都會去鏈接池獲取一個新的鏈接,效率低下 stm,_ := db.Prepare("INSERT INTO user(uid,username,age) values(?,?,?)") stm.Exec(i,"user"+strconv.Itoa(i),i-1000) stm.Close() } end = time.Now() fmt.Println("方式2 insert total time:",end.Sub(start).Seconds()) //方式3 insert start = time.Now() stm,_ := db.Prepare("INSERT INTO user(uid,username,age) values(?,?,?)") for i := 1201;i<=1300;i++{ //Exec內部並無去獲取鏈接,爲何效率仍是低呢? stm.Exec(i,"user"+strconv.Itoa(i),i-1000) } stm.Close() end = time.Now() fmt.Println("方式3 insert total time:",end.Sub(start).Seconds()) //方式4 insert start = time.Now() //Begin函數內部會去獲取鏈接 tx,_ := db.Begin() for i := 1301;i<=1400;i++{ //每次循環用的都是tx內部的鏈接,沒有新建鏈接,效率高 tx.Exec("INSERT INTO user(uid,username,age) values(?,?,?)",i,"user"+strconv.Itoa(i),i-1000) } //最後釋放tx內部的鏈接 tx.Commit() end = time.Now() fmt.Println("方式4 insert total time:",end.Sub(start).Seconds()) //方式5 insert start = time.Now() for i := 1401;i<=1500;i++{ //Begin函數每次循環內部都會去鏈接池獲取一個新的鏈接,效率低下 tx,_ := db.Begin() tx.Exec("INSERT INTO user(uid,username,age) values(?,?,?)",i,"user"+strconv.Itoa(i),i-1000) //Commit執行後鏈接也釋放了 tx.Commit() } end = time.Now() fmt.Println("方式5 insert total time:",end.Sub(start).Seconds()) // 方式1 insert total time: 3.7952171 // 方式2 insert total time: 4.3162468 // 方式3 insert total time: 4.3392482 // 方式4 insert total time: 0.3970227 // 方式5 insert total time: 7.3894226 }
database/sql接口
Go沒有官方提供數據庫驅動,而是爲開發者開發數據庫驅動定義了一些標準接口。
sql.Register
這個存在於database/sql的函數是用來註冊數據庫驅動的,當第三方開發者開發數據庫驅動時,都會實現init函數,在init裏面會調用這個Register(name string, driver driver.Driver)
完成本驅動的註冊。
咱們來看一下mymysql、sqlite3的驅動裏面都是怎麼調用的:
//https://github.com/mattn/go-sqlite3驅動 func init() { sql.Register("sqlite3", &SQLiteDriver{}) } //https://github.com/mikespook/mymysql驅動 // Driver automatically registered in database/sql var d = Driver{proto: "tcp", raddr: "127.0.0.1:3306"} func init() { Register("SET NAMES utf8") sql.Register("mymysql", &d) }
在database/sql內部經過一個map來存儲用戶定義的相應驅動。
var drivers = make(map[string]driver.Driver) drivers[name] = driver
引用第三方庫示例:
import ( "database/sql" _ "github.com/mattn/go-sqlite3" )
其中,_
就是爲了調用包的init函數。
driver.Driver
Driver是一個數據庫驅動的接口,他定義了一個method: Open(name string)
,這個方法返回一個數據庫的Conn接口。
type Driver interface { Open(name string) (Conn, error) }
返回的Conn只能用來進行一次goroutine的操做,也就是說不能把這個Conn應用於Go的多個goroutine裏面。以下代碼會出現錯誤
// ... go goroutineA (Conn) //執行查詢操做 go goroutineB (Conn) //執行插入操做 // ...
上面這樣的代碼可能會使Go不知道某個操做到底是由哪一個goroutine發起的,從而致使數據混亂,好比可能會把goroutineA裏面執行的查詢操做的結果返回給goroutineB從而使B錯誤地把此結果當成本身執行的插入數據。
第三方驅動都會定義這個函數,它會解析name參數來獲取相關數據庫的鏈接信息,解析完成後,它將使用此信息來初始化一個Conn並返回它。
driver.Conn
Conn是一個數據庫鏈接的接口定義,他定義了一系列方法,這個Conn只能應用在一個goroutine裏面,不能使用在多個goroutine裏面。
type Conn interface { Prepare(query string) (Stmt, error) Close() error Begin() (Tx, error) }
Prepare函數返回與當前鏈接相關的執行Sql語句的準備狀態,能夠進行查詢、刪除等操做。
Close函數關閉當前的鏈接,執行釋放鏈接擁有的資源等清理工做。由於驅動實現了database/sql裏面建議的conn pool,因此你不用再去實現緩存conn之類的,這樣會容易引發問題。
Begin函數返回一個表明事務處理的Tx,經過它你能夠進行查詢,更新等操做,或者對事務進行回滾、遞交。
driver.Stmt
Stmt是一種準備好的狀態,和Conn相關聯,並且只能應用於一個goroutine中,不能應用於多個goroutine。
type Stmt interface { Close() error NumInput() int Exec(args []Value) (Result, error) Query(args []Value) (Rows, error) }
Close函數關閉當前的連接狀態,可是若是當前正在執行query,query仍是有效返回rows數據。
NumInput函數返回當前預留參數的個數,當返回>=0時數據庫驅動就會智能檢查調用者的參數。當數據庫驅動包不知道預留參數的時候,返回-1。
Exec函數執行Prepare準備好的sql,傳入參數執行update/insert等操做,返回Result數據
Query函數執行Prepare準備好的sql,傳入須要的參數執行select操做,返回Rows結果集。
driver.Tx
事務處理通常就兩個過程,遞交或者回滾。數據庫驅動裏面也只須要實現這兩個函數就能夠
type Tx interface { Commit() error Rollback() error }
這兩個函數一個用來遞交一個事務,一個用來回滾事務。
driver.Execer
這是一個Conn可選擇實現的接口
type Execer interface { Exec(query string, args []Value) (Result, error) }
若是這個接口沒有定義,那麼在調用DB.Exec,就會首先調用Prepare返回Stmt,而後執行Stmt的Exec,而後關閉Stmt。
driver.Result
這個是執行Update/Insert等操做返回的結果接口定義
type Result interface { LastInsertId() (int64, error) RowsAffected() (int64, error) }
LastInsertId函數返回由數據庫執行插入操做獲得的自增ID號。
RowsAffected函數返回query操做影響的數據條目數。
driver.Rows
Rows是執行查詢返回的結果集接口定義
type Rows interface { Columns() []string Close() error Next(dest []Value) error }
Columns函數返回查詢數據庫表的字段信息,這個返回的slice和sql查詢的字段一一對應,而不是返回整個表的全部字段。
Close函數用來關閉Rows迭代器。
Next函數用來返回下一條數據,把數據賦值給dest。dest裏面的元素必須是driver.Value的值除了string,返回的數據裏面全部的string都必需要轉換成[]byte。若是最後沒數據了,Next函數最後返回io.EOF。
driver.RowsAffected
RowsAffected其實就是一個int64的別名,可是他實現了Result接口,用來底層實現Result的表示方式
type RowsAffected int64 func (RowsAffected) LastInsertId() (int64, error) func (v RowsAffected) RowsAffected() (int64, error)
driver.Value
Value其實就是一個空接口,他能夠容納任何的數據
type Value interface{}
drive的Value是驅動必須可以操做的Value,Value要麼是nil,要麼是下面的任意一種
- int64
- float64
- bool
- []byte
- string [*]除了Rows.Next返回的不能是string.
- time.Time
driver.ValueConverter
ValueConverter接口定義瞭如何把一個普通的值轉化成driver.Value的接口
type ValueConverter interface { ConvertValue(v interface{}) (Value, error) }
在開發的數據庫驅動包裏面實現這個接口的函數在不少地方會使用到,這個ValueConverter有不少好處:
- 轉化driver.value到數據庫表相應的字段,例如int64的數據如何轉化成數據庫表uint16字段
- 把數據庫查詢結果轉化成driver.Value值
- 在scan函數裏面如何把driver.Value值轉化成用戶定義的值
driver.Valuer
Valuer接口定義了返回一個driver.Value的方式
type Valuer interface { Value() (Value, error) }
不少類型都實現了這個Value方法,用來自身與driver.Value的轉化。
database/sql
database/sql在database/sql/driver提供的接口基礎上定義了一些更高階的方法,用以簡化數據庫操做,同時內部還建議性地實現一個conn pool。
type DB struct { driver driver.Driver dsn string mu sync.Mutex // protects freeConn and closed freeConn []driver.Conn closed bool }
咱們能夠看到Open函數返回的是DB對象,裏面有一個freeConn,它就是那個簡易的鏈接池。它的實現至關簡單或者說簡陋,就是當執行Db.prepare的時候會defer db.putConn(ci, err),也就是把這個鏈接放入鏈接池,每次調用conn的時候會先判斷freeConn的長度是否大於0,大於0說明有能夠複用的conn,直接拿出來用就是了,若是不大於0,則建立一個conn,而後再返回之。