Psycopg3:使用命名参数传递一个列表作为IN语句的参数



如何将列表传递给IN语句中的查询使用psycopg的命名参数?

的例子:

cur.execute("""
SELECT name
FROM users
WHERE id IN (%(ids)s)
""",
{"ids": [1, 2, 3]})

当我这样做时,我得到以下错误消息:

psycopg.errors.UndefinedFunction: operator does not exist: integer = smallint[]
HINT:  No operator matches the given name and argument type(s). You might need to add explicit type casts.

我认为你需要修改IN标准- 1️#不要使用括号来指示列表-和字典条目的类型-使用元组2️#

我没有你的表,但我确实确认了你的错误与我的一个表,在修复它之前(这是在psycopg2@2.8.6)

cursor.execute("""
SELECT *
FROM users
WHERE id IN %(ids)s 1️⃣
""",
dict(ids=(1, 2, 3)2️⃣)
)

最新更新