Python 无法构建查询字符串

标签 python mysql

我在使用以下代码时遇到问题:

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/

相关文章:

PHP:在 SQL 表上的特定行处启动 while 循环

php、mysql、打开数据库花费太长时间

python - 为什么这个空字典会破坏共享引用?

python - 正则表达式应该以不同的方式处理空格(包括换行符)

android - 简单的 kivy 选项卡示例

mysql - 无法通过 Windows PowerShell 连接到 MySQL

python - 使用索引列表访问项目列表

python 3 : What is the most efficient way to calculate all permutations of two lists summing to 100?

mysql - 无法使用 MySql 运行代码优先迁移 - RenameColumn

php - 登录页面无法使用有效凭据(PHP 和 MySQL)