Go 语言之 SQLX 高级操作 sqlx.In

Go 语言之 SQLX 高级操作 sqlx.In

sqlx.In 介绍

sqlx is a package for Go which provides a set of extensions on top of the excellent built-in database/sql package.

Illustrated guide to SQLX:http://jmoiron.github.io/sqlx/

sqlx:https://github.com/jmoiron/sqlx

"In" Queries

Because database/sql does not inspect your query and it passes your arguments directly to the driver, it makes dealing with queries with IN clauses difficult:

SELECT * FROM users WHERE level IN (?);

When this gets prepared as a statement on the backend, the bindvar ? will only correspond to a single argument, but what is often desired is for that to be a variable number of arguments depending on the length of some slice, eg:

var levels = []int{4, 6, 7}rows, err := db.Query("SELECT * FROM users WHERE level IN (?);", levels)

This pattern is possible by first processing the query with sqlx.In:

var levels = []int{4, 6, 7}query, args, err := sqlx.In("SELECT * FROM users WHERE level IN (?);", levels) // sqlx.In returns queries with the `?` bindvar, we can rebind it for our backendquery = db.Rebind(query)rows, err := db.Query(query, args...)

What sqlx.In does is expand any bindvars in the query passed to it that correspond to a slice in the arguments to the length of that slice, and then append those slice elements to a new arglist. It does this with the ? bindvar only; you can use db.Rebind to get a query suitable for your backend.

普通批量插入数据,不使用 sqlx.In

package main

import (
	"database/sql"
	"fmt"
	"strings"
	"time"

	_ "github.com/go-sql-driver/mysql" // 匿名导入 自动执行 init()
)

var db *sql.DB

func initMySQL() (err error) {
	//DSN (Data Source Name)
	dsn := "root:12345678@tcp(127.0.0.1:3306)/sql_test"
	// 注意:要初始化全局的 db 对象,不要新声明一个 db 变量
	db, err = sql.Open("mysql", dsn) // 只对格式进行校验,并不会真正连接数据库
	if err != nil {
		return err
	}

	// Ping 验证与数据库的连接是否仍处于活动状态,并在必要时建立连接。
	err = db.Ping()
	if err != nil {
		fmt.Printf("connect to db failed, err: %v\n", err)
		return err
	}
	// 数值需要根据业务具体情况来确定
	db.SetConnMaxLifetime(time.Second * 10) // 设置可以重用连接的最长时间
	db.SetConnMaxIdleTime(time.Second * 5)  // 设置连接可能处于空闲状态的最长时间
	db.SetMaxOpenConns(200)                 // 设置与数据库的最大打开连接数
	db.SetMaxIdleConns(10)                  //  设置空闲连接池中的最大连接数
	return nil
}

type User struct {
	Name string `db:"name"`
	Age  int    `db:"age"`
}

// BatchInsertUsers 批量插入数据
func BatchInsertUsers(users []*User) error {
	valueStrings := make([]string, 0, len(users))     // 占位符 slice
	valueArgs := make([]interface{}, 0, len(users)*2) // 插入值 slice

	for _, u := range users {
		valueStrings = append(valueStrings, "(?, ?)")
		valueArgs = append(valueArgs, u.Name, u.Age) // 占位符与插入值 一一对应
	}
	// 拼接完整的SQL语句
	// Sprintf根据格式说明符进行格式化,并返回结果字符串。
	// Join将其第一个参数的元素连接起来以创建单个字符串。分隔字符串sep放置在结果字符串的元素之间。
	stmt := fmt.Sprintf("INSERT INTO user (name, age) VALUES %s", strings.Join(valueStrings, ","))
	// Exec执行查询而不返回任何行。参数用于查询中的任何占位符参数。
	result, err := db.Exec(stmt, valueArgs...)
	if err != nil {
		fmt.Printf("Error inserting user into database: %v \n", err)
		return err
	}
	var rows_affected int64
	rows_affected, err = result.RowsAffected() // 返回受更新、插入或删除影响的行数。并非每个数据库或数据库驱动程序都支持此功能。
	if err != nil {
		fmt.Printf("返回受更新、插入或删除影响的行数 failed, err: %v\n", err)
		return err
	}
	fmt.Println("受更新、插入或删除影响的行数: ", rows_affected)
	return nil
}

func main() {
	if err := initMySQL(); err != nil {
		fmt.Printf("connect to db failed, err: %v\n", err)
	}
	// 检查完错误之后执行,确保 db 不为 nil
	// Close() 用来释放数据库连接相关的资源
	// Close 将关闭数据库并阻止启动新查询。关闭,然后等待服务器上已开始处理的所有查询完成。
	defer db.Close()

	fmt.Println("connect to database success")
	// db.xx() 去使用数据库操作...

	// 批量插入数据
	users := []*User{
		{Name: "刘备", Age: 25},
		{Name: "关羽", Age: 30},
		{Name: "张飞", Age: 28},
	}
	err := BatchInsertUsers(users)
	if err != nil {
		fmt.Printf("Failed to batch insert users: %v", err)
	}
}

运行

Code/go/mysql_demo via 🐹 v1.20.3 via 🅒 base 
➜ go run  main.go
connect to database success
受更新、插入或删除影响的行数:  3

Code/go/mysql_demo via 🐹 v1.20.3 via 🅒 base 
➜ 

SQL 查询结果

mysql> select * from user;  # 插入之前
+----+--------+------+
| id | name   | age  |
+----+--------+------+
|  1 | 小乔   |   12 |
|  2 | 小乔   |   22 |
|  5 | 昭君   |   18 |
|  6 | 黛玉   |   16 |
|  8 | 李煜   |   26 |
|  9 | Alice  |   25 |
| 10 | Bob    |   30 |
| 11 | Carol  |   28 |
| 12 | Alice1 |   25 |
| 13 | Bob1   |   30 |
| 14 | Carol1 |   28 |
+----+--------+------+
11 rows in set (0.00 sec)

mysql> select * from user;  # 插入之后
+----+--------+------+
| id | name   | age  |
+----+--------+------+
|  1 | 小乔   |   12 |
|  2 | 小乔   |   22 |
|  5 | 昭君   |   18 |
|  6 | 黛玉   |   16 |
|  8 | 李煜   |   26 |
|  9 | Alice  |   25 |
| 10 | Bob    |   30 |
| 11 | Carol  |   28 |
| 12 | Alice1 |   25 |
| 13 | Bob1   |   30 |
| 14 | Carol1 |   28 |
| 15 | 刘备   |   25 |
| 16 | 关羽   |   30 |
| 17 | 张飞   |   28 |
+----+--------+------+
14 rows in set (0.01 sec)

使用 sqlx.In 批量插入

package main

import (
	"database/sql/driver"
	"fmt"
	_ "github.com/go-sql-driver/mysql"
	"github.com/jmoiron/sqlx"
)

var db *sqlx.DB

func initDB() (err error) {
	dsn := "root:12345678@tcp(127.0.0.1:3306)/sql_test?charset=utf8mb4&parseTime=True"
	// 连接到数据库并使用ping进行验证。
	// 也可以使用 MustConnect MustConnect连接到数据库,并在出现错误时恐慌 panic。
	db, err = sqlx.Connect("mysql", dsn)
	if err != nil {
		fmt.Printf("connect DB failed, err:%v\n", err)
		return
	}
	db.SetMaxOpenConns(20) // 设置数据库的最大打开连接数。
	db.SetMaxIdleConns(10) // 设置空闲连接池中的最大连接数。
	return
}

type user struct {
	ID   int    `db:"id"`
	Age  int    `db:"age"`
	Name string `db:"name"`
}

func (u user) Value() (driver.Value, error) {
	return []interface{}{u.Name, u.Age}, nil
}

// BatchInsertUsersSqlxIn 使用sqlx.In帮我们拼接语句和参数, 注意传入的参数是[]interface{}
func BatchInsertUsersSqlxIn(users []interface{}) error {
	// In展开args中的切片值,返回修改后的查询字符串和一个可以由数据库执行的新的arg列表。
	// “查询”应该使用“?”“bindVar。返回值使用' ?“bindVar。
	query, args, _ := sqlx.In(
		"INSERT INTO user (name, age) VALUES (?), (?), (?)",
		users..., // 如果arg实现了 driver.Valuer, sqlx.In 会通过调用 Value()来展开它
	)
	fmt.Println("query sql string: ", query) // 查看生成的querystring
	fmt.Println("args: ", args)              // 查看生成的args
	// Exec执行查询而不返回任何行。参数用于查询中的任何占位符参数。
	result, err := db.Exec(query, args...)
	var rows_affected int64
	rows_affected, err = result.RowsAffected() // 返回受更新、插入或删除影响的行数。并非每个数据库或数据库驱动程序都支持此功能。
	if err != nil {
		fmt.Printf("返回受更新、插入或删除影响的行数 failed, err: %v\n", err)
		return err
	}
	fmt.Println("受更新、插入或删除影响的行数: ", rows_affected)
	return nil
}


func main() {
	if err := initDB(); err != nil {
		fmt.Printf("init DB failed, err:%v\n", err)
		return
	}
	fmt.Println("init DB succeeded")
	
	// 批量插入
	u1 := user{Name: "李白", Age: 16}
	u2 := user{Name: "杜甫", Age: 42}
	u3 := user{Name: "王维", Age: 29}
	users := []interface{}{u1, u2, u3}
	_ = BatchInsertUsersSqlxIn(users)
}

运行

Code/go/sqlx_demo via 🐹 v1.20.3 via 🅒 base 
➜ go run main.go
init DB succeeded
query sql string:  INSERT INTO user (name, age) VALUES (?, ?), (?, ?), (?, ?)
args:  [李白 16 杜甫 42 王维 29]
受更新、插入或删除影响的行数:  3

Code/go/sqlx_demo via 🐹 v1.20.3 via 🅒 base 

SQL 查询结果

mysql> select * from user;
+----+--------+------+
| id | name   | age  |
+----+--------+------+
|  1 | 小乔   |   12 |
|  2 | 小乔   |   22 |
|  5 | 昭君   |   18 |
|  6 | 黛玉   |   16 |
|  8 | 李煜   |   26 |
|  9 | Alice  |   25 |
| 10 | Bob    |   30 |
| 11 | Carol  |   28 |
| 12 | Alice1 |   25 |
| 13 | Bob1   |   30 |
| 14 | Carol1 |   28 |
| 15 | 刘备   |   25 |
| 16 | 关羽   |   30 |
| 17 | 张飞   |   28 |
+----+--------+------+
14 rows in set (0.01 sec)

mysql> select * from user;  # 插入之后
+----+--------+------+
| id | name   | age  |
+----+--------+------+
|  1 | 小乔   |   12 |
|  2 | 小乔   |   22 |
|  5 | 昭君   |   18 |
|  6 | 黛玉   |   16 |
|  8 | 李煜   |   26 |
|  9 | Alice  |   25 |
| 10 | Bob    |   30 |
| 11 | Carol  |   28 |
| 12 | Alice1 |   25 |
| 13 | Bob1   |   30 |
| 14 | Carol1 |   28 |
| 15 | 刘备   |   25 |
| 16 | 关羽   |   30 |
| 17 | 张飞   |   28 |
| 18 | 李白   |   16 |
| 19 | 杜甫   |   42 |
| 20 | 王维   |   29 |
+----+--------+------+
17 rows in set (0.00 sec)

mysql>

使用 NamedExec 批量插入

package main

import (
	"database/sql/driver"
	"fmt"
	_ "github.com/go-sql-driver/mysql"
	"github.com/jmoiron/sqlx"
)

var db *sqlx.DB

func initDB() (err error) {
	dsn := "root:12345678@tcp(127.0.0.1:3306)/sql_test?charset=utf8mb4&parseTime=True"
	// 连接到数据库并使用ping进行验证。
	// 也可以使用 MustConnect MustConnect连接到数据库,并在出现错误时恐慌 panic。
	db, err = sqlx.Connect("mysql", dsn)
	if err != nil {
		fmt.Printf("connect DB failed, err:%v\n", err)
		return
	}
	db.SetMaxOpenConns(20) // 设置数据库的最大打开连接数。
	db.SetMaxIdleConns(10) // 设置空闲连接池中的最大连接数。
	return
}

type user struct {
	ID   int    `db:"id"`
	Age  int    `db:"age"`
	Name string `db:"name"`
}

func (u user) Value() (driver.Value, error) {
	return []interface{}{u.Name, u.Age}, nil
}


// BatchInsertUsersNamedExec NamedExec 批量插入
func BatchInsertUsersNamedExec(users []*user) error {
	// 任何命名的占位符参数都将被arg中的字段替换。
	result, err := db.NamedExec("INSERT INTO user (name, age) VALUES (:name, :age)", users)
	var rows_affected int64
	rows_affected, err = result.RowsAffected() // 返回受更新、插入或删除影响的行数。并非每个数据库或数据库驱动程序都支持此功能。
	if err != nil {
		fmt.Printf("返回受更新、插入或删除影响的行数 failed, err: %v\n", err)
		return err
	}
	fmt.Println("BatchInsertUsersNamedExec 受插入影响的行数: ", rows_affected)
	return nil
}

func main() {
	if err := initDB(); err != nil {
		fmt.Printf("init DB failed, err:%v\n", err)
		return
	}
	fmt.Println("init DB succeeded")
	
	// 批量插入
	u1 := user{Name: "褒姒", Age: 16}
	u2 := user{Name: "貂蝉", Age: 42}
	u3 := user{Name: "飞燕", Age: 29}
	
	// NamedExec
	users := []*user{&u1, &u2, &u3}
	_ = BatchInsertUsersNamedExec(users)
}

运行

Code/go/sqlx_demo via 🐹 v1.20.3 via 🅒 base 
➜ go run main.go
init DB succeeded
BatchInsertUsersNamedExec 受插入影响的行数:  3

Code/go/sqlx_demo via 🐹 v1.20.3 via 🅒 base 
➜ 

SQL 查询结果

mysql> select * from user;
+----+--------+------+
| id | name   | age  |
+----+--------+------+
|  1 | 小乔   |   12 |
|  2 | 小乔   |   22 |
|  5 | 昭君   |   18 |
|  6 | 黛玉   |   16 |
|  8 | 李煜   |   26 |
|  9 | Alice  |   25 |
| 10 | Bob    |   30 |
| 11 | Carol  |   28 |
| 12 | Alice1 |   25 |
| 13 | Bob1   |   30 |
| 14 | Carol1 |   28 |
| 15 | 刘备   |   25 |
| 16 | 关羽   |   30 |
| 17 | 张飞   |   28 |
| 18 | 李白   |   16 |
| 19 | 杜甫   |   42 |
| 20 | 王维   |   29 |
+----+--------+------+
17 rows in set (0.00 sec)

mysql> select * from user;  # 插入之后
+----+--------+------+
| id | name   | age  |
+----+--------+------+
|  1 | 小乔   |   12 |
|  2 | 小乔   |   22 |
|  5 | 昭君   |   18 |
|  6 | 黛玉   |   16 |
|  8 | 李煜   |   26 |
|  9 | Alice  |   25 |
| 10 | Bob    |   30 |
| 11 | Carol  |   28 |
| 12 | Alice1 |   25 |
| 13 | Bob1   |   30 |
| 14 | Carol1 |   28 |
| 15 | 刘备   |   25 |
| 16 | 关羽   |   30 |
| 17 | 张飞   |   28 |
| 18 | 李白   |   16 |
| 19 | 杜甫   |   42 |
| 20 | 王维   |   29 |
| 21 | 褒姒   |   16 |
| 22 | 貂蝉   |   42 |
| 23 | 飞燕   |   29 |
+----+--------+------+
20 rows in set (0.00 sec)

mysql>

sqlx 高级操作之 IN 查询

查询 ID 在指定集合中的数据

package main

import (
	"database/sql/driver"
	"fmt"
	_ "github.com/go-sql-driver/mysql"
	"github.com/jmoiron/sqlx"
)

var db *sqlx.DB

func initDB() (err error) {
	dsn := "root:12345678@tcp(127.0.0.1:3306)/sql_test?charset=utf8mb4&parseTime=True"
	// 连接到数据库并使用ping进行验证。
	// 也可以使用 MustConnect MustConnect连接到数据库,并在出现错误时恐慌 panic。
	db, err = sqlx.Connect("mysql", dsn)
	if err != nil {
		fmt.Printf("connect DB failed, err:%v\n", err)
		return
	}
	db.SetMaxOpenConns(20) // 设置数据库的最大打开连接数。
	db.SetMaxIdleConns(10) // 设置空闲连接池中的最大连接数。
	return
}

type user struct {
	ID   int    `db:"id"`
	Age  int    `db:"age"`
	Name string `db:"name"`
}

// QueryByIDs 查询 ID 在指定集合中的数据
func QueryByIDs(ids []int) (users []user, err error) {
	// In 展开args中的切片值,返回修改后的查询字符串和一个可以由数据库执行的新的arg列表。
	// “查询”应该使用“?”“bindVar。返回值使用' ?“bindVar
	query, args, err := sqlx.In("SELECT name, age FROM user WHERE id IN (?)", ids)
	if err != nil {
		return nil, err
	}
	// Rebind 将查询从 QUESTION 转换为DB驱动程序的 bindvar 类型。
	query = db.Rebind(query)
	// Select 使用此数据库。任何占位符参数都将被提供的参数替换。
	err = db.Select(&users, query, args...)
	if err != nil {
		return nil, err
	}
	return users, nil
}

func main() {
	if err := initDB(); err != nil {
		fmt.Printf("init DB failed, err:%v\n", err)
		return
	}
	fmt.Println("init DB succeeded")
	
	// IN 查询
	users, err := QueryByIDs([]int{1, 15, 21, 2})  // 默认按照主键顺序排列
	if err != nil {
		fmt.Printf("query error: %v\n", err)
		return
	}
	fmt.Printf("query successful result users %v\n", users)
	for _, user := range users {
		fmt.Printf("user: %#v\n", user)
	}
}

运行
Code/go/sqlx_demo via 🐹 v1.20.3 via 🅒 base 
➜ go run main.go
init DB succeeded
query successful result users [{0 12 小乔} {0 22 小乔} {0 25 刘备} {0 16 褒姒}]
user: main.user{ID:0, Age:12, Name:"小乔"}
user: main.user{ID:0, Age:22, Name:"小乔"}
user: main.user{ID:0, Age:25, Name:"刘备"}
user: main.user{ID:0, Age:16, Name:"褒姒"}

Code/go/sqlx_demo via 🐹 v1.20.3 via 🅒 base 
➜ 

SQL 查询结果

mysql> select * from user;
+----+--------+------+
| id | name   | age  |
+----+--------+------+
|  1 | 小乔   |   12 |
|  2 | 小乔   |   22 |
|  5 | 昭君   |   18 |
|  6 | 黛玉   |   16 |
|  8 | 李煜   |   26 |
|  9 | Alice  |   25 |
| 10 | Bob    |   30 |
| 11 | Carol  |   28 |
| 12 | Alice1 |   25 |
| 13 | Bob1   |   30 |
| 14 | Carol1 |   28 |
| 15 | 刘备   |   25 |
| 16 | 关羽   |   30 |
| 17 | 张飞   |   28 |
| 18 | 李白   |   16 |
| 19 | 杜甫   |   42 |
| 20 | 王维   |   29 |
| 21 | 褒姒   |   16 |
| 22 | 貂蝉   |   42 |
| 23 | 飞燕   |   29 |
+----+--------+------+
20 rows in set (0.00 sec)

mysql>
  • 查询结果默认按照主键顺序排列
  • 自定义查询结果顺序
    • 使用代码排序
    • 使用 MySQL 排序 FIND_IN_SET

sqlx 高级操作之 FIND_IN_SET

package main

import (
	"database/sql/driver"
	"fmt"
	_ "github.com/go-sql-driver/mysql"
	"github.com/jmoiron/sqlx"
	"strings"
)

var db *sqlx.DB

func initDB() (err error) {
	dsn := "root:12345678@tcp(127.0.0.1:3306)/sql_test?charset=utf8mb4&parseTime=True"
	// 连接到数据库并使用ping进行验证。
	// 也可以使用 MustConnect MustConnect连接到数据库,并在出现错误时恐慌 panic。
	db, err = sqlx.Connect("mysql", dsn)
	if err != nil {
		fmt.Printf("connect DB failed, err:%v\n", err)
		return
	}
	db.SetMaxOpenConns(20) // 设置数据库的最大打开连接数。
	db.SetMaxIdleConns(10) // 设置空闲连接池中的最大连接数。
	return
}

type user struct {
	ID   int    `db:"id"`
	Age  int    `db:"age"`
	Name string `db:"name"`
}

// QueryByIDs  查询 ID 在指定集合中的数据
func QueryByIDs(ids []int) (users []user, err error) {
	// In 展开args中的切片值,返回修改后的查询字符串和一个可以由数据库执行的新的arg列表。
	// “查询”应该使用“?”“bindVar。返回值使用' ?“bindVar
	query, args, err := sqlx.In("SELECT name, age FROM user WHERE id IN (?)", ids)
	if err != nil {
		return nil, err
	}
	// Rebind 将查询从 QUESTION 转换为DB驱动程序的 bindvar 类型。
	query = db.Rebind(query)
	// Select 使用此数据库。任何占位符参数都将被提供的参数替换。
	err = db.Select(&users, query, args...)
	if err != nil {
		return nil, err
	}
	return users, nil
}

// QueryAndOrderByIDs 根据 ID 在指定集合中和指定顺序查询
func QueryAndOrderByIDs(ids []int) (users []user, err error) {
	// 创建一个字符串切片,大小为ids的长度
	strIDs := make([]string, 0, len(ids))
	// 将ids转换为字符串类型
	for _, id := range ids {
		// Sprintf根据格式说明符进行格式化,并返回结果字符串。
		strIDs = append(strIDs, fmt.Sprintf("%d", id))
	}
	// In展开args中的切片值,返回修改后的查询字符串和一个可以由数据库执行的新的arg列表。“查询”应该使用“?”“bindVar。返回值使用' ?“bindVar。
	query, args, err := sqlx.In("SELECT name, age FROM user WHERE id IN (?) ORDER BY FIND_IN_SET(id, ?)", ids, strings.Join(strIDs, ","))
	if err != nil {
		return
	}

	// Rebind 将查询从QUESTION转换为DB驱动程序的bindvar类型。
	query = db.Rebind(query)
	// 执行查询 Select 使用此数据库。任何占位符参数都将被提供的参数替换。
	err = db.Select(&users, query, args...)
	return
}

func main() {
	if err := initDB(); err != nil {
		fmt.Printf("init DB failed, err:%v\n", err)
		return
	}
	fmt.Println("init DB succeeded")
	
	// IN 查询
	users, err := QueryByIDs([]int{1, 15, 21, 2})
	if err != nil {
		fmt.Printf("query error: %v\n", err)
		return
	}
	fmt.Printf("query successful result users %v\n", users)
	for _, user := range users {
		fmt.Printf("user: %#v\n", user)
	}

	fmt.Println("**************")
	// FIND_IN_SET
	users, err = QueryAndOrderByIDs([]int{1, 15, 21, 2})
	if err != nil {
		fmt.Printf("query error: %v\n", err)
		return
	}
	fmt.Printf("query successful result users %v\n", users)
	for _, user := range users {
		fmt.Printf("user: %#v\n", user)
	}
}

运行

Code/go/sqlx_demo via 🐹 v1.20.3 via 🅒 base 
➜ go run main.go
init DB succeeded
query successful result users [{0 12 小乔} {0 22 小乔} {0 25 刘备} {0 16 褒姒}]
user: main.user{ID:0, Age:12, Name:"小乔"}
user: main.user{ID:0, Age:22, Name:"小乔"}
user: main.user{ID:0, Age:25, Name:"刘备"}
user: main.user{ID:0, Age:16, Name:"褒姒"}
**************
query successful result users [{0 12 小乔} {0 25 刘备} {0 16 褒姒} {0 22 小乔}]
user: main.user{ID:0, Age:12, Name:"小乔"}  # FIND_IN_SET 按照指定顺序查询
user: main.user{ID:0, Age:25, Name:"刘备"}
user: main.user{ID:0, Age:16, Name:"褒姒"}
user: main.user{ID:0, Age:22, Name:"小乔"}

Code/go/sqlx_demo via 🐹 v1.20.3 via 🅒 base 
➜ 

注意:开发中,使用代码排序还是使用 SQL FIND_IN_SET 查询排序,需要根据开发实际情况来使用。

官方示例

package main

import (
    "database/sql"
    "fmt"
    "log"
    
    _ "github.com/lib/pq"
    "github.com/jmoiron/sqlx"
)

var schema = `
CREATE TABLE person (
    first_name text,
    last_name text,
    email text
);

CREATE TABLE place (
    country text,
    city text NULL,
    telcode integer
)`

type Person struct {
    FirstName string `db:"first_name"`
    LastName  string `db:"last_name"`
    Email     string
}

type Place struct {
    Country string
    City    sql.NullString
    TelCode int
}

func main() {
    // this Pings the database trying to connect
    // use sqlx.Open() for sql.Open() semantics
    db, err := sqlx.Connect("postgres", "user=foo dbname=bar sslmode=disable")
    if err != nil {
        log.Fatalln(err)
    }

    // exec the schema or fail; multi-statement Exec behavior varies between
    // database drivers;  pq will exec them all, sqlite3 won't, ymmv
    db.MustExec(schema)
    
    tx := db.MustBegin()
    tx.MustExec("INSERT INTO person (first_name, last_name, email) VALUES ($1, $2, $3)", "Jason", "Moiron", "jmoiron@jmoiron.net")
    tx.MustExec("INSERT INTO person (first_name, last_name, email) VALUES ($1, $2, $3)", "John", "Doe", "johndoeDNE@gmail.net")
    tx.MustExec("INSERT INTO place (country, city, telcode) VALUES ($1, $2, $3)", "United States", "New York", "1")
    tx.MustExec("INSERT INTO place (country, telcode) VALUES ($1, $2)", "Hong Kong", "852")
    tx.MustExec("INSERT INTO place (country, telcode) VALUES ($1, $2)", "Singapore", "65")
    // Named queries can use structs, so if you have an existing struct (i.e. person := &Person{}) that you have populated, you can pass it in as &person
    tx.NamedExec("INSERT INTO person (first_name, last_name, email) VALUES (:first_name, :last_name, :email)", &Person{"Jane", "Citizen", "jane.citzen@example.com"})
    tx.Commit()

    // Query the database, storing results in a []Person (wrapped in []interface{})
    people := []Person{}
    db.Select(&people, "SELECT * FROM person ORDER BY first_name ASC")
    jason, john := people[0], people[1]

    fmt.Printf("%#v\n%#v", jason, john)
    // Person{FirstName:"Jason", LastName:"Moiron", Email:"jmoiron@jmoiron.net"}
    // Person{FirstName:"John", LastName:"Doe", Email:"johndoeDNE@gmail.net"}

    // You can also get a single result, a la QueryRow
    jason = Person{}
    err = db.Get(&jason, "SELECT * FROM person WHERE first_name=$1", "Jason")
    fmt.Printf("%#v\n", jason)
    // Person{FirstName:"Jason", LastName:"Moiron", Email:"jmoiron@jmoiron.net"}

    // if you have null fields and use SELECT *, you must use sql.Null* in your struct
    places := []Place{}
    err = db.Select(&places, "SELECT * FROM place ORDER BY telcode ASC")
    if err != nil {
        fmt.Println(err)
        return
    }
    usa, singsing, honkers := places[0], places[1], places[2]
    
    fmt.Printf("%#v\n%#v\n%#v\n", usa, singsing, honkers)
    // Place{Country:"United States", City:sql.NullString{String:"New York", Valid:true}, TelCode:1}
    // Place{Country:"Singapore", City:sql.NullString{String:"", Valid:false}, TelCode:65}
    // Place{Country:"Hong Kong", City:sql.NullString{String:"", Valid:false}, TelCode:852}

    // Loop through rows using only one struct
    place := Place{}
    rows, err := db.Queryx("SELECT * FROM place")
    for rows.Next() {
        err := rows.StructScan(&place)
        if err != nil {
            log.Fatalln(err)
        } 
        fmt.Printf("%#v\n", place)
    }
    // Place{Country:"United States", City:sql.NullString{String:"New York", Valid:true}, TelCode:1}
    // Place{Country:"Hong Kong", City:sql.NullString{String:"", Valid:false}, TelCode:852}
    // Place{Country:"Singapore", City:sql.NullString{String:"", Valid:false}, TelCode:65}

    // Named queries, using `:name` as the bindvar.  Automatic bindvar support
    // which takes into account the dbtype based on the driverName on sqlx.Open/Connect
    _, err = db.NamedExec(`INSERT INTO person (first_name,last_name,email) VALUES (:first,:last,:email)`, 
        map[string]interface{}{
            "first": "Bin",
            "last": "Smuth",
            "email": "bensmith@allblacks.nz",
    })

    // Selects Mr. Smith from the database
    rows, err = db.NamedQuery(`SELECT * FROM person WHERE first_name=:fn`, map[string]interface{}{"fn": "Bin"})

    // Named queries can also use structs.  Their bind names follow the same rules
    // as the name -> db mapping, so struct fields are lowercased and the `db` tag
    // is taken into consideration.
    rows, err = db.NamedQuery(`SELECT * FROM person WHERE first_name=:first_name`, jason)
    
    
    // batch insert
    
    // batch insert with structs
    personStructs := []Person{
        {FirstName: "Ardie", LastName: "Savea", Email: "asavea@ab.co.nz"},
        {FirstName: "Sonny Bill", LastName: "Williams", Email: "sbw@ab.co.nz"},
        {FirstName: "Ngani", LastName: "Laumape", Email: "nlaumape@ab.co.nz"},
    }

    _, err = db.NamedExec(`INSERT INTO person (first_name, last_name, email)
        VALUES (:first_name, :last_name, :email)`, personStructs)

    // batch insert with maps
    personMaps := []map[string]interface{}{
        {"first_name": "Ardie", "last_name": "Savea", "email": "asavea@ab.co.nz"},
        {"first_name": "Sonny Bill", "last_name": "Williams", "email": "sbw@ab.co.nz"},
        {"first_name": "Ngani", "last_name": "Laumape", "email": "nlaumape@ab.co.nz"},
    }

    _, err = db.NamedExec(`INSERT INTO person (first_name, last_name, email)
        VALUES (:first_name, :last_name, :email)`, personMaps)
}

更多示例请参考官方文档https://github.com/jmoiron/sqlx/blob/master/sqlx_test.go

热门相关:流鱼无恙   法医娇宠,扑倒傲娇王爷   横行霸道   朕是红颜祸水   重生当学神,又又又考第一了!