以下是我的脚本的一部分:
sql1 = """
SELECT distinct th.name, 'MMS' as Status
FROM t1
JOIN t2 on t1.id = t2.tid
WHERE t2.station= 'MatS'
AND t1.name IN ({listthinglist})
""".format(listthinglist=', '.join("'" + item + "'" for item in list_of_things))
cur.execute(sql1)
list_of_things
包含22016个条目,我无法在python中运行此查询。有没有其他方法可以在python中运行这样的查询。
发布于 2018-06-02 06:49:12
请不要为此使用format
。execute
函数接受一个'parameters‘参数,您可以将列表传递给该参数。您可以这样修改它:
sql1 = """
SELECT distinct th.name, 'MMS' as Status
FROM t1
JOIN t2 on t1.id = t2.tid
WHERE t2.station= 'MatS'
AND t1.name IN (%s)
"""
cur.execute(sql1, (list_of_things,))
https://stackoverflow.com/questions/50651691
复制相似问题