热门IT资讯网

用sqlx库连接mysql操作方法

发表于:2024-11-28 作者:热门IT资讯网编辑
编辑最后更新 2024年11月28日,本文主要给大家介绍用sqlx库连接mysql操作方法,文章内容都是笔者用心摘选和编辑的,具有一定的针对性,对大家的参考意义还是比较大的,下面跟笔者一起了解下用sqlx库连接mysql操作方法吧。连接m

本文主要给大家介绍用sqlx库连接mysql操作方法,文章内容都是笔者用心摘选和编辑的,具有一定的针对性,对大家的参考意义还是比较大的,下面跟笔者一起了解下用sqlx库连接mysql操作方法吧。

连接mysql(用sqlx库,不仅支持mysql,第一个参数还可以是别的数据库):
database, err := sqlx.Open("mysql", "root:password@tcp(127.0.0.1:3306)/dbname")
insert操作:
r, err := Db.Exec("insert into person(username, sex, email)values(?, ?, ?)", "stu001", "man", "[email protected]")

package mainimport (    "fmt"    _ "github.com/go-sql-driver/mysql"    "github.com/jmoiron/sqlx")type Person struct {    UserId   int    `db:"user_id"`    Username string `db:"username"`    Sex      string `db:"sex"`    Email    string `db:"email"`}type Place struct {    Country string `db:"country"`    City    string `db:"city"`    TelCode int    `db:"telcode"`}//此处的db是线程安全的,实现了连接池var Db *sqlx.DBfunc init() {    database, err := sqlx.Open("mysql", "root:@tcp(127.0.0.1:3306)/test")    if err != nil {        fmt.Println("open mysql failed,", err)        return    }    Db = database}func main() {    r, err := Db.Exec("insert into person(username, sex, email)values(?, ?, ?)", "stu001", "man", "[email protected]")    if err != nil {        fmt.Println("exec failed, ", err)        return    }    id, err := r.LastInsertId()    if err != nil {        fmt.Println("exec failed, ", err)        return    }    fmt.Println("insert succ:", id)}

Select 操作:
err := Db.Select(&person, "select user_id, username, sex, email from person where user_id=?", 1)

package mainimport (    "fmt"    _ "github.com/go-sql-driver/mysql"    "github.com/jmoiron/sqlx")type Person struct {    UserId   int    `db:"user_id"`    Username string `db:"username"`    Sex      string `db:"sex"`    Email    string `db:"email"`}type Place struct {    Country string `db:"country"`    City    string `db:"city"`    TelCode int    `db:"telcode"`}var Db *sqlx.DBfunc init() {    database, err := sqlx.Open("mysql", "root:@tcp(127.0.0.1:3306)/test")    if err != nil {        fmt.Println("open mysql failed,", err)        return    }    Db = database}func main() {    var person []Person    err := Db.Select(&person, "select user_id, username, sex, email from person where user_id=?", 1)    if err != nil {        fmt.Println("exec failed, ", err)        return    }    fmt.Println("select succ:", person)}

update操作:
_, err := Db.Exec("update person set username=? where user_id=?", "stu0001", 1)

package mainimport (    "fmt"    _ "github.com/go-sql-driver/mysql"    "github.com/jmoiron/sqlx")type Person struct {    UserId   int    `db:"user_id"`    Username string `db:"username"`    Sex      string `db:"sex"`    Email    string `db:"email"`}type Place struct {    Country string `db:"country"`    City    string `db:"city"`    TelCode int    `db:"telcode"`}var Db *sqlx.DBfunc init() {    database, err := sqlx.Open("mysql", "root:@tcp(127.0.0.1:3306)/test")    if err != nil {        fmt.Println("open mysql failed,", err)        return    }    Db = database}func main() {    _, err := Db.Exec("update person set username=? where user_id=?", "stu0001", 1)    if err != nil {        fmt.Println("exec failed, ", err)        return    }}

Delete 操作:
_, err := Db.Exec("delete from person where user_id=?", 1)

package mainimport (    "fmt"    _ "github.com/go-sql-driver/mysql"    "github.com/jmoiron/sqlx")type Person struct {    UserId   int    `db:"user_id"`    Username string `db:"username"`    Sex      string `db:"sex"`    Email    string `db:"email"`}type Place struct {    Country string `db:"country"`    City    string `db:"city"`    TelCode int    `db:"telcode"`}var Db *sqlx.DBfunc init() {    database, err := sqlx.Open("mysql", "root:@tcp(127.0.0.1:3306)/test")    if err != nil {        fmt.Println("open mysql failed,", err)        return    }    Db = database}func main() {    _, err := Db.Exec("delete from person where user_id=?", 1)    if err != nil {        fmt.Println("exec failed, ", err)        return    }    fmt.Println("delete succ")
}

看完以上关于用sqlx库连接mysql操作方法,很多读者朋友肯定多少有一定的了解,如需获取更多的行业知识信息 ,可以持续关注我们的行业资讯栏目的。
0