我正在使用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 工具运行相同的查询时,一切运行良好,我得到了结果。问题出在哪里?
你传递了错误的参数,你应该给出一个元组、一个列表或一个字典
元组示例:
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