使用Psycopg2模块执行查询时出现以下错误。 我已经在PgAdmin 4中执行了相同的查询,并且运行正常,并给出了结果。当我通过Python执行该错误时,就会发生错误。
有人可以建议我如何解决这个问题吗?
下面是代码:
for new_ticket in zenpy_client.search(type='ticket', status='open',requester='je.rr.y@gmail.com'):
#new_ticket.assignee = modified_user
#print(new_ticket.requester.email)
psql_command=""" WITH CTE AS (select cast(origin as text) as origin, MAX(case when name like '%OUT%' THEN name else ''end) as out_ref
, MAX(case when name like '%PICK%' THEN name else ''end) as pick_ref
, MAX(case when name like '%PICK%' THEN state else ''end) as pick_state
, MAX(case when name like '%OUT%' THEN state else ''end) as out_state
from dl_odoo.stock_picking
GROUP BY origin)
select o.customer_email,o.order_number,o.order_state_1,o.order_state_2,o.order_subtotal_net_after_discount as revenue
,created_at_order,c.out_ref as reference,o.payment_method,c.pick_ref,c.pick_state,c.out_ref,c.out_state
from ol.orders o
LEFT JOIN CTE c ON c.origin=o.order_number
where
customer_email='%s' order by o.created_at_order desc limit 3
;"""
try:
s=''
print(new_ticket.requester.email)
print(psql_command)
try:
psql_cursor.execute(psql_command %new_ticket.requester.email)
except (Exception, psycopg2.Error) as error:
print ("ssss")
print (error)
答案 0 :(得分:1)
您需要转义字符串中的百分号。 Python将它们解释为好像是类似于printf的格式说明符。用%%替换每个%,它应该可以工作。