连接Mysql
- 访问此网站搜索MySQL
- 第一个就是按照指引运行
go get -u github.com\go-sql-driver\mysql
- 导入包建立连接
package main
import (
"database/sql"
"fmt"
"time"
_ "github.com/go-sql-driver/mysql"
)
var db *sql.DB
func initdb() (err error) {
db, err = sql.Open("mysql", "ellis:ellis@tcp(192.168.214.134:3306)/go_db?charset=utf8mb4&parseTime=True")
if err != nil {
return err
}
err = db.Ping()
if err != nil {
return err
}
db.SetConnMaxLifetime(time.Minute * 3)
db.SetMaxOpenConns(10)
db.SetMaxIdleConns(10)
return nil
}
func main() {
err := initdb()
if err != nil {
panic(err)
}
}
插入数据
func(db *DB) Exec(query string,args...interface{})(Result,error)
package main
import (
"database/sql"
"fmt"
"time"
_ "github.com/go-sql-driver/mysql"
)
var db *sql.DB
func initdb() (err error) {
db, err = sql.Open("mysql", "ellis:ellis@tcp(192.168.214.134:3306)/go_db?charset=utf8mb4&parseTime=True")
if err != nil {
return err
}
err = db.Ping()
if err != nil {
return err
}
db.SetConnMaxLifetime(time.Minute * 3)
db.SetMaxOpenConns(10)
db.SetMaxIdleConns(10)
return nil
}
func insertValue(name string, password string) (result sql.Result, err error) {
query := "insert into user_tbl(name,password) values(?,?)"
result, err = db.Exec(query, name, password)
if err != nil {
fmt.Printf("err: %v\n", err)
}
fmt.Printf("result: %v\n", result)
fmt.Println(result.LastInsertId())
return
}
func main() {
err := initdb()
if err != nil {
panic(err)
}
insertValue("ellis", "ellis")
}
查询
- 单行查询使用QueryRow,详见selectOne函数
package main
import (
"database/sql"
"fmt"
"time"
_ "github.com/go-sql-driver/mysql"
)
var db *sql.DB
func initdb() (err error) {
db, err = sql.Open("mysql", "ellis:ellis@tcp(192.168.214.134:3306)/go_db?charset=utf8mb4&parseTime=True")
if err != nil {
return err
}
err = db.Ping()
if err != nil {
return err
}
db.SetConnMaxLifetime(time.Minute * 3)
db.SetMaxOpenConns(10)
db.SetMaxIdleConns(10)
return nil
}
type User struct {
id int
name string
password string
}
func searchOne() {
var user User
db.QueryRow("select * from user_tbl where id=1").Scan(&user.id, &user.name, &user.password)
fmt.Printf("user.id: %v\n", user.id)
}
func main() {
err := initdb()
if err != nil {
panic(err)
}
searchOne()
}
package main
import (
"database/sql"
"fmt"
"time"
_ "github.com/go-sql-driver/mysql"
)
var db *sql.DB
func initdb() (err error) {
db, err = sql.Open("mysql", "ellis:ellis@tcp(192.168.214.134:3306)/go_db?charset=utf8mb4&parseTime=True")
if err != nil {
return err
}
err = db.Ping()
if err != nil {
return err
}
db.SetConnMaxLifetime(time.Minute * 3)
db.SetMaxOpenConns(10)
db.SetMaxIdleConns(10)
return nil
}
type User struct {
id int
name string
password string
}
func searchAll() {
query := "select * from user_tbl"
r, err := db.Query(query)
defer r.Close()
if err != nil {
fmt.Printf("err: %v\n", err)
} else {
for r.Next() {
var u User
r.Scan(&u.id, &u.name, &u.password)
fmt.Printf("u: %v\n", u)
}
}
}
func main() {
err := initdb()
if err != nil {
panic(err)
}
fmt.Printf("db: %v\n", db)
searchAll()
}
更新
package main
import (
"database/sql"
"fmt"
"time"
_ "github.com/go-sql-driver/mysql"
)
var db *sql.DB
func initdb() (err error) {
db, err = sql.Open("mysql", "ellis:ellis@tcp(192.168.214.134:3306)/go_db?charset=utf8mb4&parseTime=True")
if err != nil {
return err
}
err = db.Ping()
if err != nil {
return err
}
db.SetConnMaxLifetime(time.Minute * 3)
db.SetMaxOpenConns(10)
db.SetMaxIdleConns(10)
return nil
}
type User struct {
id int
name string
password string
}
func update() {
sql := "update user_tbl set name=? where id=?"
r, err := db.Exec(sql, "vv", 1)
if err != nil {
fmt.Printf("err: %v\n", err)
} else {
fmt.Println(r.RowsAffected())
}
}
func main() {
err := initdb()
if err != nil {
panic(err)
}
update()
}
删除
package main
import (
"database/sql"
"fmt"
"time"
_ "github.com/go-sql-driver/mysql"
)
var db *sql.DB
func initdb() (err error) {
db, err = sql.Open("mysql", "ellis:ellis@tcp(192.168.214.134:3306)/go_db?charset=utf8mb4&parseTime=True")
if err != nil {
return err
}
err = db.Ping()
if err != nil {
return err
}
db.SetConnMaxLifetime(time.Minute * 3)
db.SetMaxOpenConns(10)
db.SetMaxIdleConns(10)
return nil
}
type User struct {
id int
name string
password string
}
func delete() {
sql := "delete from user_tbl where id=?"
r, err := db.Exec(sql, 1)
if err != nil {
fmt.Printf("err: %v\n", err)
} else {
fmt.Println(r.RowsAffected())
}
}
func main() {
err := initdb()
if err != nil {
panic(err)
}
delete()
}