如何在SQL脚本的“where”子句中传递Python dataframe列值?
我正在尝试用Python运行Oracle SQL脚本。我有一个包含列Week的dataframe df1,运行。我想使用包含run_number = RUNi的“where”子句运行SQL脚本。我在Python语言中有一个带有多个cur.execute(sql_query)语句的'for‘循环。在其中一个'sql_query‘语句中,我有两个' where’子句,其中我必须过滤掉一个run_number。然而,我得到了‘绑定变量不支持在DDL语句中’的错误。这个问题的解决方案是什么?
for i in range(len(df1['RUN'])):
cur.execute(sql_query_1, (int(df1['RUN'][i]), int(df1['RUN'][i])))
cur.execute(sql_query_2)
sql_query_1 ='''Create table ....
where E.CID=A.CID AND :1 BETWEEN E.RUN_START AND E.RUN_END AND RUN = :2
'''我期望'for‘循环遍历所有运行值,运行一系列查询,生成特定的临时表集合并附加到Python中的最终dataframe对象。
发布于 2020-01-05 20:43:37
请尝试如下所示:
for i in range(len(df1['RUN'])):
cur.execute(sql_query_1, m=int(df1['RUN'][i]), n=int(df1['RUN'][i]))
cur.execute(sql_query_2)
sql_query_1 ='''Create table ....
where E.CID=A.CID AND E.RUN_START between :m and :n and E.RUN_END between :m and:n
'''https://stackoverflow.com/questions/55997743
复制相似问题