Sqlx: unsupported type []int, a slice of int

Created on 22 Aug 2017  路  2Comments  路  Source: jmoiron/sqlx

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.

Most helpful comment

@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...)

See: https://godoc.org/github.com/jmoiron/sqlx#In

All 2 comments

@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...)

See: https://godoc.org/github.com/jmoiron/sqlx#In

Sorry, it's my mistake, after try the in method, it work well! 馃憤

Was this page helpful?
0 / 5 - 0 ratings

Related issues

huygn picture huygn  路  6Comments

yzk0281 picture yzk0281  路  5Comments

fpolli picture fpolli  路  5Comments

davidxiao picture davidxiao  路  5Comments

johnknapp picture johnknapp  路  5Comments