`UPDATE "dd"."number" SET number_val=? WHERE id=?`
已经定义了一个var number_info []numberType的切片,数据也已经放进去了
number_info[0].number_val = 123
number_info[0].id = 8
number_info[1].number_val = 99
number_info[1].id = 10
我想批量更新要怎么处理呢,库用的是sqlx,查了很久没找到
应该不需要使用for循环来一条一条组合的执行吧
数据库使用的是postgresql
可以试一下sqlx.in
```
func (u User) Value() (driver.Value, error) {
return []interface{}{u.Name, u.Age}, nil
}
```
```
query, args, _ := sqlx.In(
"INSERT INTO user (name, age) VALUES (?), (?), (?)",
users..., // 如果arg实现了 driver.Valuer, sqlx.In 会通过调用 Value()来展开它
)
fmt.Println(query) // 查看生成的querystring
fmt.Println(args) // 查看生成的args
_, err := DB.Exec(query, args...)
```
#2
更多评论
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)
然后使用ON duplicate KEY UPDATE试试
#1