postgresql sql: 转换参数 $1 类型: 不支持的类型 []int, in

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/53983170/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 02:44:53  来源:igfitidea点击:

sql: converting argument $1 type: unsupported type []int, a slice of in

postgresqlgo

提问by Gabriel A. Zorrilla

Have this:

拥有这个:

somevars := []int{1, 2, 3, 4}
rows, err = db.Query("SELECT c1,c2 FROM table"+tid+" WHERE c1 IN(,,,);", somevars)

Got this:

明白啦:

sql: converting argument $1 type: unsupported type []int, a slice of int

sql: 转换参数 $1 类型: 不支持的类型 []int, int 的一个切片

Any way to make a slice of arguments work with lib/pq?

有什么方法可以使一部分参数与 lib/pq 一起使用?

回答by Gabriel A. Zorrilla

pq.Array was the answer:

pq.Array 是答案:

somevars := []int{1, 2, 3, 4}
rows, err = db.Query("SELECT c1,c2 FROM table"+tid+" WHERE c1 = any();", pq.Array(somevars))

回答by nos

An alternative solution is

另一种解决方案是

somevars := []interface{}{1, 2, 3, 4}
rows, err = db.Query(
    "SELECT c1,c2 FROM table"+tid+" WHERE c1 IN(,,,);",
    somevars...)

Here the ...expands a slice to multiple arguments, similar to the python *args. It's documented in the language spec.

这里...将切片扩展为多个参数,类似于 python *args。它记录在语言规范中

The db.QueryAPI supports this so called variadic parameter.

db.QueryAPI支持这个所谓的可变参数的参数。

func (db *DB) Query(query string, args ...interface{}) (*Rows, error)

Here interface{}is known as the empty interface, and it can hold values of any type. See the Go tour example here. So one can use it like

这里interface{}被称为空接口,它可以保存任何类型的值。请参阅此处Go tour 示例。所以一个人可以像这样使用它

db.Query(stmt, var1, var2)

db.Query(stmt, var1, var2)

where var1var2could be of different types.

wherevar1var2可能是不同的类型。

In your case, you can also pass the slice elements explicitly

在您的情况下,您还可以显式传递切片元素

db.Query(stmt,
         somevars[0], somevars[1], somevars[2], somevars[3])

But it is rather verbose and requires extra work when the slice length changes.

但它相当冗长,当切片长度改变时需要额外的工作。

Note that if instead of the interfaceslice somevars, we use intvars := []int {1, 2, 3, 4}and expand intvarsin db.Query(), the compiler will complain on intvars...

请注意,如果我们使用并扩展in而不是interface切片somevars,编译器将抱怨intvars := []int {1, 2, 3, 4}intvarsdb.Query()intvars...

cannot use []int literal (type []int) as type []interface {} in assignment

不能在赋值中使用 []int 文字(类型 []int)作为类型 []interface {}

Type conversion intvars.([]interface{})doesn't work either. This is documented in the language spec FAQ. And there is also a dedicated wiki page for it

类型转换intvars.([]interface{})也不起作用。这在语言规范 FAQ 中有记录。还有一个专门的维基页面

It is disallowed by the language specification because the two types do not have the same representation in memory.

语言规范不允许这样做,因为这两种类型在内存中没有相同的表示。

The intuitive picture of golang interfaceis an object with two fields, one field stores a type, and the other stores a pointer.

golang的直观图interface是一个对象有两个字段,一个字段存储一个类型,另一个存储一个指针。