golang sqlx用法笔记

11 篇文章 0 订阅

golang sqlx用法笔记
原创westhod 发布于2018-07-26 15:05:58 阅读数 5674  收藏
展开

在上一篇文章中吐槽了golang 数据库查询接口(https://mp.csdn.net/postedit/80799266),后来在网上找到了sqlx这个第三方库,用起来确实爽多了,这里记录下学习和用法的心得

安装:
使用命令即可

go get github.com/jmoiron/sqlx
介绍:
官方的介绍如下:

sqlx is a library which provides a set of extensions on go's standard database/sql library. 
The sqlx versions of sql.DB, sql.TX, sql.Stmt, et al. all leave the underlying interfaces untouched, 
so that their interfaces are a superset on the standard ones. 
This makes it relatively painless to integrate existing codebases using database/sql with sqlx.
 
Major additional concepts are:
 
Marshal rows into structs (with embedded struct support), maps, and slices
Named parameter support including prepared statements
Get and Select to go quickly from query to struct/slice
In addition to the godoc API documentation, there is also some standard documentation that explains 
how to use database/sql along with sqlx.
大意就是sqlx是golang 标准database/sql的扩展,使用sqlx的接口跟原先的接口方法没什么两样,但有如下扩展:

1.可将行记录映射如struct(内嵌struct也支持),map与slices          <--这正是我之前想要的效果

2.支持在preprared statement 中使用命名参数

3.Get 和Select的查询结果到struct/slice更快速

sqlx也增加了许多接口,方便开发者使用,后面会讲到。

使用:
还是上篇文章的案例,不过,这次插入数据交给go代码来实现,便于说明sqlx方法的使用,go代码如下

package main
 
import (
    "database/sql"
    _"github.com/go-sql-driver/mysql"
    "github.com/jmoiron/sqlx"
    "log"
    "fmt"
)
 
type Student struct {
    Id         int    `db:"id"`
    Name       string `db:"name"`
    Nick       string `db:"nick"`
    Country    string `db:"country"`
    Province   string `db:"province"`
    City       string `db:"city"`
    ImgUrl     string `db:"img_url"`
    Status     int    `db:"status"`
    CreateTime string `db:"create_time"`
}
 
func main()  {
    dns := fmt.Sprintf("%s:%s@tcp(%s)/%s?charset=utf8", dbuser, dbpwd, dbhost, dbname)
    db, err := sqlx.Connect("mysql", dns)
    if err != nil {
        log.Fatalln(err)
    }
    defer db.Close()
 
    tx := db.MustBegin()
    tx.MustExec(`INSERT INTO student VALUES ('1', 'Jack', 'Jack', 'England', '', '', 'http://img2.imgtn.bdimg.com/it/u=3588772980,2454248748&fm=27&gp=0.jpg', '1', '2018-06-26 17:08:35');`)
    tx.MustExec(`INSERT INTO student VALUES ('2', 'Emily', 'Emily', 'England', '', '', 'http://img2.imgtn.bdimg.com/it/u=3588772980,2454248748&fm=27&gp=0.jpg', '2', null);`)
    err = tx.Commit()
    if err != nil {
        log.Fatalln(err)
    }
 
}
生成数据表的代码如下:

DROP TABLE IF EXISTS `student`;
CREATE TABLE `student` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(64) NOT NULL,
  `nick` varchar(64) DEFAULT NULL,
  `country` varchar(128) DEFAULT NULL,
  `province` varchar(64) DEFAULT NULL,
  `city` varchar(64) DEFAULT NULL,
  `img_url` varchar(256) DEFAULT NULL,
  `status` int(11) DEFAULT NULL,
  `create_time` varchar(32) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
sqlx.Connect("mysql", dns)内部实现了Open()和Ping(),因此调用Connect()方法后,则可直接使用该对象进行数据库操作,这里开启一个事务,插入2条数据,代码运行非常成功。假如把插入的第二条语句修改为:

tx.MustExec(`INSERT INTO student VALUES ('2', null, 'Emily', 'England', '', '', 'http://img2.imgtn.bdimg.com/it/u=3588772980,2454248748&fm=27&gp=0.jpg', '2', null);`)
运行程序报

panic: Error 1048: Column 'name' cannot be null
再看数据表中没有插入数据,表明事务没有被提交。这些方法用起来感觉还是好多了!这里的方法名中的Must应当是multi-statement的缩写,而不是英文must(必须)的意思。

tx.MustExec()函数也可以带参数,跟其他语言的语法很像,比如:

tx.MustExec(`INSERT INTO student (id, name, nick, country, province, city, img_url, status) VALUES (?, ?, ?, ?, ?, ?, ?, ?);`, "3", "Jobs", "Jobs", "America", "", "", "http://img2.imgtn.bdimg.com/it/u=3588772980,2454248748&fm=27&gp=0.jpg", "3")
也可使用命名参数,然后直接传一个struct对象进去:

    s := Student {
        Id : 4,
        Name : "Cook", 
        Nick : "Cook",
        Country : "America",
        Province : "", 
        City : "",
        ImgUrl : "http://img2.imgtn.bdimg.com/it/u=3588772980,2454248748&fm=27&gp=0.jpg",
        Status : 1,
    }
 
    _, err = tx.NamedExec(`INSERT INTO student VALUES (:id, :name, :nick, :country, :province, :city, :img_url, :status, :create_time);`, s)
    if err != nil {
        log.Fatalln(err)
    }
这里的命名参数需要跟struct的tag对应,假如把字段Name的tag修改为:`db:"username"`,运行时报错:

ould not find name name in main.Student{Id:4, Name:"Cook", Nick:"Cook", Country:"America", Province:"", City:"", ImgUrl:"http://img2.imgtn.bdimg.com/it/u=3588772980,2454248748&fm=27&gp=0.jpg", Status:1, CreateTime:sql.NullString{String:"", Valid:false}}
现在,我要查询数据了,按条件查询一条记录:

    var s Student
    err = db.Get(&s, "SELECT * FROM student WHERE name=?", "Cook")
    if err != nil {
        log.Fatalln(err)
    }
 
    fmt.Println(s)
程序运行正常,这样使用实在太方便不过了,现在我要查询多条记录:

    var stds [] Student
    db.Select(&stds, "SELECT * FROM student")
 
    for i, v := range stds {
        fmt.Printf("%d--->%v\n", i+1, v)
    }
发现只打印了一条记录,怎么回事?加上错误打印看看:

    var stds [] Student
    err = db.Select(&stds, "SELECT * FROM student")
    if err != nil {
        log.Fatalln(err)
    }
 
    for i, v := range stds {
        fmt.Printf("%d--->%v\n", i+1, v)
    }
运行时报:

sql: Scan error on column index 8: unsupported Scan, storing driver.Value type <nil> into type *string
大意是,在Scan index为8即第9个字段时出错(索引从0开始计算),不支持的Scan,数据库字段为nil转为 *string时出错。仔细想想,在插入记录时,只有第一条记录的create_time字段是有值的,其他都是默认为NULL,数据库也是设计为可空的,再查看官方使用示例时,也提到:

if you have null fields and use SELECT *, you must use sql.Null* in your struct
即如果你有null字段且使用SELECT *,你的结构体定义中,字段需使用类型sql.Null*,create_time是字符串,所以需定义为sql.NullString,另外还有其他类型:sql.NullBool、sql.NullFloat64、sql.NullInt64。赶紧屁颠屁颠把Student的struct修改如下:

type Student struct {
    Id         int    `db:"id"`
    Name       string `db:"name"`
    Nick       sql.NullString `db:"nick"`
    Country    sql.NullString `db:"country"`
    Province   sql.NullString `db:"province"`
    City       sql.NullString `db:"city"`
    ImgUrl     sql.NullString `db:"img_url"`
    Status     sql.NullInt64  `db:"status"`
    CreateTime sql.NullString `db:"create_time"`
}
由于name字段在设计时,是不可空的,所以这里仍然定义为string类型。

现在程序能正常运行,打印4条记录了。

也可以使用Queryx把行数据查询出来,然后再Scan到struct中:

    rows, err := db.Queryx("SELECT * FROM student")
    if err != nil {
        fmt.Println(err)
        panic(err)
    }
 
    for rows.Next() {
        var stu Student
        err := rows.StructScan(&stu)
        if err != nil {
            log.Fatalln(err)
        }
 
        fmt.Println(stu)
    }
相对来说,还是Select简洁得多了。
————————————————
版权声明:本文为CSDN博主「westhod」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/westhod/article/details/81205758

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值