下載驅動
go get -u github.com/go-sql-driver/mysql
連接資料庫
package main
import (
"database/sql"
"fmt"
_ "github.com/go-sql-driver/mysql"
)
var db *sql.DB
func initDB() (err error) {
db, err = sql.Open("mysql", "root:root1234@tcp(127.0.0.1:3306)/hello")
if err != nil {
return err
}
err = db.Ping()
if err != nil {
return err
}
return nil
}
func main() {
err := initDB()
if err != nil {
fmt.Printf("init db failed,err:%v\n", err)
return
}
}
最大連線數
package main
import (
"database/sql"
_ "github.com/go-sql-driver/mysql"
"log"
)
func main() {
db, err = sql.Open("mysql", "root:root1234@tcp(127.0.0.1:3306)/hello")
if err != nil {
return err
}
defer db.Close()
db.DB().SetMaxOpenConns(100)
}
最大閒置連線數
package main
import (
"database/sql"
_ "github.com/go-sql-driver/mysql"
"log"
)
func main() {
db, err = sql.Open("mysql", "root:root1234@tcp(127.0.0.1:3306)/hello")
if err != nil {
return err
}
defer db.Close()
db.DB().SetMaxIdleConns(10)
}
查詢
package main
import (
"database/sql"
"fmt"
_ "github.com/go-sql-driver/mysql"
)
var db *sql.DB
type User struct {
Uid int
Name string
Phone string
}
var u User
func queryRow() {
err := db.QueryRow("select uid,name,phone from `user` where uid=?", 1).Scan(&u.Uid, &u.Name, &u.Phone)
if err != nil {
fmt.Printf("scan failed, err:%v\n", err)
return
}
fmt.Printf("uid:%d name:%s phone:%s\n", u.Uid, u.Name, u.Phone)
}
func main() {
db, err = sql.Open("mysql", "root:root1234@tcp(127.0.0.1:3306)/hello")
if err != nil {
return err
}
queryRow()
}
查詢多條資料
package main
import (
"database/sql"
"fmt"
_ "github.com/go-sql-driver/mysql"
)
var db *sql.DB
type User struct {
Uid int
Name string
Phone string
}
var u User
func queryMultiRow() {
rows, err := db.Query("select uid,name,phone from `user` where uid > ?", 0)
if err != nil {
fmt.Printf("query failed, err:%v\n", err)
return
}
defer rows.Close()
for rows.Next() {
err := rows.Scan(&u.Uid, &u.Name, &u.Phone)
if err != nil {
fmt.Printf("scan failed, err:%v\n", err)
return
}
fmt.Printf("uid:%d name:%s phone:%s\n", u.Uid, u.Name, u.Phone)
}
}
func main() {
db, err = sql.Open("mysql", "root:root1234@tcp(127.0.0.1:3306)/hello")
if err != nil {
return err
}
queryMultiRow()
}
插入
package main
import (
"database/sql"
"fmt"
_ "github.com/go-sql-driver/mysql"
)
var db *sql.DB
type User struct {
Uid int
Name string
Phone string
}
var u User
func insertRow() {
ret, err := db.Exec("insert into user(username,phone) values (?,?)", "王五", 13988557766)
if err != nil {
fmt.Printf("insert failed, err:%v\n", err)
return
}
uid, err := ret.LastInsertId()
if err != nil {
fmt.Printf("get lastinsert ID failed, err:%v\n", err)
return
}
fmt.Printf("insert success, the id is %d.\n", uid)
}
func main() {
db, err = sql.Open("mysql", "root:root1234@tcp(127.0.0.1:3306)/hello")
if err != nil {
return err
}
insertRow()
}
更新
package main
import (
"database/sql"
"fmt"
_ "github.com/go-sql-driver/mysql"
)
var db *sql.DB
type User struct {
Uid int
Name string
Phone string
}
var u User
func updateRow() {
ret, err := db.Exec("update user set username=? where uid = ?", "张三", 3)
if err != nil {
fmt.Printf("update failed, err:%v\n", err)
return
}
n, err := ret.RowsAffected()
if err != nil {
fmt.Printf("get RowsAffected failed, err:%v\n", err)
return
}
fmt.Printf("update success, affected rows:%d\n", n)
}
func main() {
db, err = sql.Open("mysql", "root:root1234@tcp(127.0.0.1:3306)/hello")
if err != nil {
return err
}
updateRow()
}
刪除
package main
import (
"database/sql"
"fmt"
_ "github.com/go-sql-driver/mysql"
)
var db *sql.DB
type User struct {
Uid int
Name string
Phone string
}
var u User
func deleteRow() {
ret, err := db.Exec("delete from user where uid = ?", 2)
if err != nil {
fmt.Printf("delete failed, err:%v\n", err)
return
}
n, err := ret.RowsAffected()
if err != nil {
fmt.Printf("get RowsAffected failed, err:%v\n", err)
return
}
fmt.Printf("delete success, affected rows:%d\n", n)
}
func main() {
db, err = sql.Open("mysql", "root:root1234@tcp(127.0.0.1:3306)/hello")
if err != nil {
return err
}
deleteRow()
}