pg8000.core.ProgrammingError:'无法确定参数$ 2的数据类型'

时间:2018-03-12 16:39:46

标签: postgresql pg8000

我正在使用pg.8000(Postgres)并尝试运行以下SELECT查询

cursor.execute(
        """
        SELECT orders.name, orders.order_price, orders.selling_price, orders.earnings
        FROM member, orders
        WHERE member.id=orders.member_id
        AND member.name = %s
        """,
        member_username
    )

member.username是一个字符串。

但是我收到以下错误。

  

pg8000.core.ProgrammingError :('ERROR','ERROR','42P18','无法确定参数$ 2的数据类型','postgres.c','1350','exec_parse_message','', '')

然而,当我使用GUI工具运行相同的查询时,一切运行正常,我得到了结果。有什么问题?

1 个答案:

答案 0 :(得分:0)

你传递了错误的参数,你应该给出一个元组,一个列表或一个字典

元组示例:

cursor.execute(
        """
        SELECT orders.name, orders.order_price, orders.selling_price, orders.earnings
        FROM member, orders
        WHERE member.id=orders.member_id
        AND member.name = %s
        """,
        (member_username,)
    )

列表示例:

cursor.execute(
        """
        SELECT orders.name, orders.order_price, orders.selling_price, orders.earnings
        FROM member, orders
        WHERE member.id=orders.member_id
        AND member.name = %s
        """,
        [member_username]
    )

字典示例:

cursor.execute(
        """
        SELECT orders.name, orders.order_price, orders.selling_price, orders.earnings
        FROM member, orders
        WHERE member.id=orders.member_id
        AND member.name = %(mname)s
        """,
        {'mname' : member_username}
    )

http://initd.org/psycopg/docs/usage.html#query-parameters