我在使用以下代码时遇到问题:
start_date = dt.datetime(2012,01,01,00,00,00)
end_date = dt.datetime(2014,01,01,00,00,00)
sql="SELECT c.id extension, er.id, flow, filename, filesize, unread, er.cr_date, callerid, \
length, callid, info, party FROM extension_recording er, extension e, client c \
WHERE er.extension_id = e.id AND e.client_id = c.id AND c.parent_client_id = %s \
AND er.cr_date => %s AND er.cr_date <= %s" % (client_id) (start_date) (end_date)
cur.execute(sql)
recordings = cur.fetchall()
if recordings.rowcount == 0: sys.exit("No recordings for selected date range - exiting")
for recording in recordings:
do stuff to recording
构建查询字符串会导致以下错误:
TypeError: 'str' object is not callable
我确信我遗漏了一些非常明显的东西,但我只见树木不见森林。
最佳答案
将长行的末尾更改为:
... er.cr_date <= %s" % (client_id, start_date, end_date)
此外,当您使用它时,三重引号对于长行更方便:
sql = """
SELECT c.id extension, ...
... er.cr_date <= %s""" % (...
关于Python 无法构建查询字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14282557/