使用命名参数将列表作为 IN 语句的参数传递[重复]

问题描述 投票:0回答:1

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

示例:

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.
python psycopg3
1个回答
-2
投票

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

我没有您的桌子,但在修复之前我确实用我的一张桌子确认了您的错误(这是在 [电子邮件受保护]

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


© www.soinside.com 2019 - 2024. All rights reserved.