【发布时间】:2021-05-09 12:11:08
【问题描述】:
我正在尝试在 Python 的循环中将查询传递给 sql:
for i in superstar.iloc[:, 0]:
cursor = conn.cursor()
loop_cmd = f"""
DROP TABLE IF EXISTS superstar_fwdPlusbwd_{i}; \
CREATE TABLE superstar_fwdPlusbwd_{i} \
SELECT a.*, b.CitingPatPublNr as fwd_CitingPatPublNr, c.CitedPatPublNr as bwd_CitedPatPublNr \
from orbisFirm_focals_basic_feed a \
left join patents.Patents_ForwardCitations b on a.focal_PatPublNr = b.PatPublNr \
left join patents.Patents_BackwardCitations c on a.focal_PatPublNr = c.PatPublNr \
where focal_PatPublNr = "{i}"
;
"""
cursor.execute(loop_cmd)
但这会引发语法错误:
ProgrammingError: (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'CREATE TABLE `superstar_fwdPlusbwd_AU2002310193B2` SELECT a.*, b.CitingPatPu' at line 1")
这似乎是 MySql 中的语法错误,但此查询在 MySQL Workbench 中有效。请帮忙,谢谢!
【问题讨论】:
-
看起来不支持批处理(多查询)。将其作为 2 个单独的查询执行。而且我没有看到
{i}替换(这似乎是不正确的)。 -
@Akina 谢谢!多查询是问题!我知道这一定是愚蠢的哈哈。请将您的 cmets 放在 Answer 中,以便我接受!
-
最好自己创建答案 - 详细,参考并引用文档和代码示例...将其视为咨询费用。
标签: python mysql select create-table