如何使用sqlx在切片中查询MySQL?

huangapple go评论171阅读模式
英文:

How to use sqlx to query mysql IN a slice?

问题

我想查询MySQL数据库中一个表中在切片中的值:

var qids []int
// 动态填充 qids
err = database.SQL.Select(&quotes,
    "SELECT * FROM quote WHERE qid IN $1", qids)
if err != nil {
    log.Println(err)
}

但是我得到了这个错误:

sql: converting Exec argument #0's type: unsupported type []int, a slice
quotes []

我该如何修复这个问题?

英文:

I want to query a table in mysql database for values IN a slice:

var qids []int
//fill qids dynamically
err = database.SQL.Select(&quotes,
	"SELECT * FROM quote WHERE qid IN $1", qids)
if err != nil {
	log.Println(err)
}

But I get this error:

sql: converting Exec argument #0's type: unsupported type []int, a slice
quotes []

How can I fix this?

答案1

得分: 27

sqlx有一个很好的辅助函数:In(),我们只需要通过获取参数并重新绑定来准备查询,就像这样:

var qids []int

// 动态填充qids
query, args, err := sqlx.In("SELECT * FROM quote WHERE qid IN (?)", qids)
if err != nil {
    log.Fatal(err)
}

// sqlx.In 返回带有 `?` 绑定变量的查询,我们可以重新绑定它以适应我们的后端
query = database.SQL.Rebind(query)  // database.SQL 应该是 *sqlx.DB

err = database.SQL.Select(&quotes, query, args...)
if err != nil {
    log.Fatal(err)
}

// 或者只用一行代码:

err = database.SQL.Select(&quotes, database.SQL.Rebind(query), args...)

另外,我建议你在这里查看:http://jmoiron.github.io/sqlx/,里面有很多包括 IN 的示例。

英文:

sqlx has a great helper for that: In() we just have to prepare the query by taking the args and Rebind, like this:

var qids []int

// fills qids on query dynamically
query, args, err := sqlx.In("SELECT * FROM quote WHERE qid IN (?)", qids)
if err != nil {
	log.Fatal(err)
}

// sqlx.In returns queries with the `?` bindvar, we can rebind it for our backend
//
query = database.SQL.Rebind(query)  // database.SQL should be a *sqlx.DB

err = database.SQL.Select(&quotes, query, args...)
if err != nil {
	log.Fatal(err)
}

// or just in one line:

err = database.SQL.Select(&quotes, database.SQL.Rebind(query), args...)

Also I recommend you take a look here: http://jmoiron.github.io/sqlx/ there're a lot of examples including IN

答案2

得分: -2

标签表明您正在使用sqlx。它在查询中支持IN操作。

因此,您可以这样做:

var qids []int
// 动态填充 qids
rows, err = db.Queryx("SELECT * FROM quote WHERE qid IN (?)", qids)
if err != nil {
    log.Println(err)
}
// 扫描行数据
英文:

The tags suggest that you are using sqlx. It has support for IN in queries.

So you can do

var qids []int
//fill qids dynamically
rows, err = db.Query(&quotes, "SELECT * FROM quote WHERE qid IN ($1)", qids)
if err != nil {
    log.Println(err)
}
// scan the rows

答案3

得分: -3

嘿,这是因为 []int 类型的原因
尝试这样做:

type Int64Array []int64

// Value 返回与驱动程序兼容的值
func (a Int64Array) Value() (driver.Value, error) {
    var strs []string
    for _, i := range a {
        strs = append(strs, strconv.FormatInt(i, 10))
    }
    return "{" + strings.Join(strs, ",") + "}", nil
}

然后将 int64 传递给查询

db.Queryx("SELECT * FROM quote WHERE qid IN $1", int64IDs)
**更多详细信息请查看[这里][1]**

  [1]: http://www.dammitjim.co.uk/blog/golang-using-pq-sqlx-slices/
英文:

Hey its because of []int
try this

type Int64Array []int64

// Value returns the driver compatible value
func (a Int64Array) Value() (driver.Value, error) {
var strs []string
for _, i := range a {
	strs = append(strs, strconv.FormatInt(i, 10))
}
return "{" + strings.Join(strs, ",") + "}", nil
}

and then pass int64 to query

db.Queryx("SELECT * FROM quote WHERE qid IN $1", int64IDs)

for more detail check here

huangapple
  • 本文由 发表于 2016年11月13日 01:44:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/40565805.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定