01go操作mysql.go
package main
import (
"database/sql"
"fmt"
_ "github.com/go-sql-driver/mysql"
)
var db *sql.DB
func initDB() (err error) {
dsn := "root:LiuChang@=123456@tcp(121.199.57.215:3306)/gorm01?charset=utf8&parseTime=True"
db, err = sql.Open("mysql", dsn)
if err != nil {
return err
}
err = db.Ping()
if err != nil {
return err
}
return nil
}
type user struct {
id int
age int
name string
}
func queryRowDemo() {
sqlStr := "select id, name, age from user where id=?"
var u user
err := db.QueryRow(sqlStr, 1).Scan(&u.id, &u.name, &u.age)
if err != nil {
fmt.Printf("scan failed, err:%v\n", err)
return
}
fmt.Printf("id:%d name:%s age:%d\n", u.id, u.name, u.age)
}
func queryMultiRowDemo() {
sqlStr := "select id, name, age from user where id > ?"
rows, err := db.Query(sqlStr, 1)
if err != nil {
fmt.Printf("query failed, err:%v\n", err)
return
}
defer func(rows *sql.Rows) {
err = rows.Close()
if err != nil {
fmt.Printf("rows close failed,err:%v\n", err)
return
}
}(rows)
for rows.Next() {
var u user
err = rows.Scan(&u.id, &u.name, &u.age)
if err != nil {
fmt.Printf("scan failed, err:%v\n", err)
return
}
fmt.Printf("id:%d name:%s age:%d\n", u.id, u.name, u.age)
}
}
func insertRowDemo() {
sqlStr := "insert into user(name, age) values (?,?)"
ret, err := db.Exec(sqlStr, "wangWu", 15)
if err != nil {
fmt.Printf("insert failed, err:%v\n", err)
return
}
theID, 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", theID)
}
func updateRowDemo() {
sqlStr := "update user set age=? where id = ?"
ret, err := db.Exec(sqlStr, 28, 5)
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 deleteRowDemo() {
sqlStr := "delete from user where id = ?"
ret, err := db.Exec(sqlStr, 5)
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 prepareQueryDemo() {
sqlStr := "select id, name, age from user where id > ?"
stmt, err := db.Prepare(sqlStr)
if err != nil {
fmt.Printf("prepare failed, err:%v\n", err)
return
}
defer func(stmt *sql.Stmt) {
err = stmt.Close()
if err != nil {
fmt.Printf("prepare close failed,err:%v\n", err)
return
}
}(stmt)
rows, err := stmt.Query(0)
if err != nil {
fmt.Printf("query failed, err:%v\n", err)
return
}
defer func(rows *sql.Rows) {
err = rows.Close()
if err != nil {
fmt.Printf("rows close failed,err:%v\n", err)
return
}
}(rows)
for rows.Next() {
var u user
err = rows.Scan(&u.id, &u.name, &u.age)
if err != nil {
fmt.Printf("scan failed, err:%v\n", err)
return
}
fmt.Printf("id:%d name:%s age:%d\n", u.id, u.name, u.age)
}
}
func prepareInsertDemo() {
sqlStr := "insert into user(name,age) values(?,?)"
stmt, err := db.Prepare(sqlStr)
if err != nil {
fmt.Printf("prepare failed, err:%v\n", err)
return
}
defer func(stmt *sql.Stmt) {
err = stmt.Close()
if err != nil {
fmt.Printf("prepare close failed, err:%v\n", err)
return
}
}(stmt)
ret, err := stmt.Exec("paoPao", 22)
if err != nil {
fmt.Printf("insert failed err:%v\n", err)
return
}
thId, 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", thId)
}
func main() {
err := initDB()
db.SetMaxOpenConns(10)
db.SetMaxIdleConns(10)
if err != nil {
fmt.Printf("init db failed,err:%v\n", err)
return
}
fmt.Println("mysql数据库连接成功!")
}
02go使用sqlx操作mysql.go
package main
import (
"fmt"
_ "github.com/go-sql-driver/mysql"
"github.com/jmoiron/sqlx"
)
var db02 *sqlx.DB
func initDB02() (err error) {
dsn := "root:LiuChang@=123456@tcp(121.199.57.215:3306)/gorm01?charset=utf8&parseTime=True"
db02, err = sqlx.Connect("mysql", dsn)
if err != nil {
fmt.Printf("connect DB failed, err:%v\n", err)
return
}
db02.SetMaxOpenConns(20)
db02.SetMaxIdleConns(10)
return
}
type user02 struct {
ID int `db:"id"`
Name string `db:"name"`
Age int `db:"age"`
}
func queryRowDemo02() {
sqlStr := "select id, name, age from user where id=?"
var u user02
err := db02.Get(&u, sqlStr, 1)
if err != nil {
fmt.Printf("get failed, err:%v\n", err)
return
}
fmt.Printf("id:%d name:%s age:%d\n", u.ID, u.Name, u.Age)
}
func queryMultiRowDemo02() {
sqlStr := "select id, name, age from user where id > ?"
var users []user02
err := db02.Select(&users, sqlStr, 0)
if err != nil {
fmt.Printf("query failed, err:%v\n", err)
return
}
fmt.Printf("users:%#v\n", users)
}
func insertRowDemo02() {
sqlStr := "insert into user(name, age) values (?,?)"
ret, err := db02.Exec(sqlStr, "Tom", 25)
if err != nil {
fmt.Printf("insert failed, err:%v\n", err)
return
}
theID, 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", theID)
}
func updateRowDemo02() {
sqlStr := "update user set age=? where id = ?"
ret, err := db02.Exec(sqlStr, 27, 7)
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 deleteRowDemo02() {
sqlStr := "delete from user where id = ?"
ret, err := db02.Exec(sqlStr, 7)
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 insertUserDemo02() {
sqlStr := "INSERT INTO user (name,age) VALUES (:name,:age)"
ret, err := db02.NamedExec(sqlStr,
map[string]interface{}{
"name": "Jerry",
"age": 26,
})
if err != nil {
fmt.Printf("insert failed, err:%v\n", err)
return
}
theID, 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", theID)
}
func namedQuery02() {
sqlStr := "SELECT * FROM user WHERE name=:name"
rows, err := db02.NamedQuery(sqlStr, map[string]interface{}{"name": "wangEr"})
if err != nil {
fmt.Printf("db.NamedQuery failed, err:%v\n", err)
return
}
defer func(rows *sqlx.Rows) {
err = rows.Close()
if err != nil {
fmt.Printf("db.NamedQuery close failed, err:%v\n", err)
return
}
}(rows)
for rows.Next() {
var u user02
err = rows.StructScan(&u)
if err != nil {
fmt.Printf("scan failed, err:%v\n", err)
return
}
fmt.Printf("user:%#v\n", u)
}
}
func namedQuery021() {
u := user02{
Name: "zhangSan",
}
sqlStr := "SELECT * FROM user WHERE name=:name"
rows, err := db02.NamedQuery(sqlStr, &u)
if err != nil {
fmt.Printf("db.NamedQuery failed, err:%v\n", err)
return
}
defer func(rows *sqlx.Rows) {
err = rows.Close()
if err != nil {
fmt.Printf("db.NamedQuery close failed, err:%v\n", err)
return
}
}(rows)
for rows.Next() {
var u user02
err = rows.StructScan(&u)
if err != nil {
fmt.Printf("scan failed, err:%v\n", err)
return
}
fmt.Printf("user:%#v\n", u)
}
}
func batchInsertUsers(users []*user02) {
sqlStr := "INSERT INTO user (name, age) VALUES (:name, :age)"
ret, err := db02.NamedExec(sqlStr, users)
if err != nil {
fmt.Printf("insert 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("insert success, affected rows:%d\n", n)
}
func main() {
err := initDB02()
if err != nil {
fmt.Println(err)
return
}
fmt.Println("连接mysql数据库成功!")
u1 := user02{Name: "jinGang", Age: 16}
u2 := user02{Name: "neZha", Age: 17}
u3 := user02{Name: "huLuWa", Age: 18}
users := []*user02{&u1, &u2, &u3}
batchInsertUsers(users)
}