【发布时间】:2020-02-25 11:07:36
【问题描述】:
我正在尝试使用 cx_oracle 在 python 中插入数据库(Oracle)。我需要从表中选择并插入到另一个表中。
insert_select_string = "INSERT INTO wf_measure_details(PARENT_JOB_ID, STAGE_JOB_ID, MEASURE_VALS, STEP_LEVEL, OOZIE_JOB_ID, CREATE_TIME_TS) \
select PARENT_JOB_ID, STAGE_JOB_ID, MEASURE_VALS, STEP_LEVEL, OOZIE_JOB_ID, CREATE_TIME_TS from wf_measure_details_stag where oozie_job_id = '{0}'.format(self.DAG_id)"
conn.executemany(insert_select_string)
conn.commit()
insert_count = conn.rowcount
但我遇到了错误。我没有数据的选择参数,因为数据是从选择查询中获取的。
Required argument 'parameters' (pos 2) not found
请提出解决方法
【问题讨论】:
-
首先阅读手册cx-oracle.readthedocs.io/en/latest/user_guide/… 并查看示例。不要使用 '{0}'.format(self.DAG_id),因为它存在安全风险和性能问题。仅当您将多个绑定值作为来自 Python 的数据传递时才调用 executemany() 方法 - 您不是。
-
@ChristopherJones 我写了格式,因为我不知道如何在插入语句中绑定变量,其中值来自 select 命令。您共享的文档链接没有插入+选择的示例。
-
看低一点:cx-oracle.readthedocs.io/en/latest/user_guide/…。无论您使用简单的插入语句还是带有选择语句的插入,都没有任何区别。
标签: python-2.7 insert cx-oracle