【发布时间】:2019-02-06 22:01:50
【问题描述】:
我生成了一个 ID 号列表。我想执行一个插入语句,从一个表中获取 ID 值在我的列表中的所有记录,并将这些记录插入到另一个表中。
我发现了这个 cx_Oracle 函数,而不是运行多个执行语句(据我所知是可能的),它据说可以使用单个语句和列表参数执行所有内容。 (它还避免了在传递参数之前 SQL 语句的笨拙格式)但是我认为我需要在将列表作为参数传递之前更改它。只是不确定如何。
我引用了这个网页: https://dev.mysql.com/doc/connector-python/en/connector-python-api-mysqlcursor-executemany.html
ids = getIDs()
print(ids)
[('12345',),('24567',),('78945',),('65423',)]
sql = """insert into scheme.newtable
select id, data1, data2, data3
from scheme.oldtable
where id in (%s)"""
cursor.prepare(sql)
cursor.executemany(None, ids)
我希望 SQL 语句执行如下:
插入到 scheme.newtable select id, data1, data2, data3 from scheme.oldtable where id in ('12345','24567','78945','65423')
相反,我收到以下错误: ORA-01036: 非法变量名称/编号
编辑: 我发现了这个 StackOverflow:How can I do a batch insert into an Oracle database using Python? 我更新了我的代码以预先准备语句并将列表项更新为元组,但我仍然遇到同样的错误。
【问题讨论】:
标签: python-2.7 cx-oracle