我正在尝试编写一个采用列表参数(即作为值列表的单个参数)的查询。看来这在 PostgreSQL 中至少有时是可能的(https://stackoverflow.com/a/10829760/836390 https://stackoverflow.com/a/10829760/836390)。我想要的是这样的:
rows, err := db.Query("SELECT * FROM table WHERE id in $1", []int{1, 2, 3})
但是,当我使用pq https://github.com/lib/pq驱动程序,我收到错误:
sql: converting Exec argument #0's type: unsupported type []int, a slice
这是否根本不支持pq
还没有,或者这不支持database/sql
,或者根本不在 PostgreSQL 中,或者什么?
您可以使用pq.Array https://github.com/lib/pq/blob/master/array.go现在带有切片参数。所以查询看起来像:
rows, err := db.Query("SELECT * FROM table WHERE id in $1", pq.Array([]int{1, 2, 3}))
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)