【发布时间】:2015-02-03 08:23:14
【问题描述】:
这是一段给我带来麻烦的代码
def process_message( msg):
# deserialize msg
id_dict = json.loads(msg)
# extract id
report_id = id_dict['id']
print report_id
sql = """ select r.id,
format('DATA "the_geom from (%s) as subquery
using unique gid using srid=4326"', replace(replace(sql,'<<','%%'),'>>','%%')) as data,
rcode,
format(' VALIDATION
''userid'' ''^\d+$''
%s
END',string_agg(format (' ''%s'' ''%s''', paramname, prompt[1]),'
')) as validation
FROM report.reports r
JOIN report.reportparams rp on r.id = rp.reportid and not rp.del
where r.id = %s
group by r.id, sql, rcode;"""
args = ('%s','%s','%s','%s','%s')
cursor.execute(sql, args)
row = cursor.fetchone()
(id,data,rcode,validation) = row
print (id,data,rcode,validation)
exit
运行代码时出现的错误信息是这样的
Traceback (most recent call last):
File "mapfile_queue_processor.py", line 60, in <module>
process_message( content )
File "mapfile_queue_processor.py", line 41, in process_message
cursor.execute(sql, args)
psycopg2.ProgrammingError: type "s" does not exist
LINE 2: format('DATA "the_geom from ('%s') as subquery
^
现在,我根据人们之前的建议尝试了一些不同的修复方法,但都没有奏效。
在 sql 变量中,我尝试将所有 %s 设置为 %%s 和 '%s' 和 '%%s' 和 "%s" 和 "%%s" 甚至是 {s}其中
我似乎找到的唯一可能的解决方案是我不能有args = ('%s','%s','%s','%s','%s'),我需要有实际参数而不是'%s'
这是我的问题的解决方案吗? 如果是这样,我该怎么做?
如果不是解决方案,我该如何解决?
【问题讨论】:
标签: python sql python-2.7 psycopg2