使用go-sqlx库怎么批量更新

yz23je · · 3124 次点击
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
更多评论
可以试一下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