英文:
How to use sqlx to query mysql IN a slice?
问题
我想查询MySQL数据库中一个表中在切片中的值:
var qids []int
// 动态填充 qids
err = database.SQL.Select("es,
"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("es, query, args...)
if err != nil {
log.Fatal(err)
}
// 或者只用一行代码:
err = database.SQL.Select("es, 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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论