My code like below:
ids := []int{}
for _, idc := range idcs {
ids = append(ids, idc.Id)
}
log.Printf("%v\n", ids)
bws := []Bw{}
sql := "select * from idc_bw where idc_id in (?);"
rows, err := cdnops.DbConn.Queryx(sql, ids)
if err != nil {
log.Errorf("error on when executing sql: %s, value: (%s), error: %v", sql, ids, err)
return nil, err
}
then raise some error:
sql: converting Exec argument $1 type: unsupported type []int, a slice of int
I create a mysql container in my mac, sql_version is mysql:5.7.
@buhuipao try the In method:
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 backend
query = db.Rebind(query)
rows, err := db.Query(query, args...)
Sorry, it's my mistake, after try the in method, it work well! 馃憤
Most helpful comment
@buhuipao try the
Inmethod:See: https://godoc.org/github.com/jmoiron/sqlx#In