Python:在python中使用executemany()批量上传Oracle表的记录
Posted
技术标签:
【中文标题】Python:在python中使用executemany()批量上传Oracle表的记录【英文标题】:Python: Bulk upload records of Oracle table with executemany() in python 【发布时间】:2020-10-22 22:09:07 【问题描述】: data=[]
dataToInsert = [ ]
for index, row in df.iterrows():
contentid = row['CONTENTID']
Objectsummary = row['OBJECT_SUMMARY']
Title=row['TITLE']
if Title is None:
Title = ""
if Objectsummary is None:
Objectsummary = ""
allSummeries =Title + ' ' + Objectsummary
lists=function_togetNounsAndVerbs(allSummeries)
verbList =lists[0]
nounList =lists[1]
NounSet = set(nounList)
VerbSet = set(verbList)
verbs = " "
verbs=verbs.join(VerbSet)
nouns=" "
nouns=nouns.join(NounSet)
verbs=re.sub(r" ", ", ", verbs)
nouns=re.sub(r" ", ", ", nouns)
# Here we are going to create the data sdet to be updated in database table in batch form.
data.append(nouns)
data.append(verbs)
data.append('PROCESSED')
data.append(contentid)
dataToInsert.append([data[0], data[1], data[2], data[3]])
print("ALL DATA TO BE UPDATED IN TABLE IS :---> ",dataToInsert)
statement = """UPDATE test_batch_update_python SET NOUNS = ?, Verbs = ? where CONTENTID = ?"""
a = cursor.executemany(statement, dataToInsert)
connection.commit()
在上面的代码中 function_togetNounsAndVerbs(allSummeries) 这个函数将返回列表。 我收到以下异常:
**a = cursor.executemany(statement, dataToInsert)
cx_Oracle.DatabaseError: ORA-01036: illegal variable name/number**
请帮我解决这个问题。 或者我还有什么其他方法可以做到这一点。最初,我曾经使用 cursor.execute() 一次更新单行,但这非常耗时。为了尽量减少我使用批量上传的时间(即 cursor.executemany() )
【问题讨论】:
您的查询中有 3 个占位符:UPDATE test_batch_update_python SET NOUNS = ?, Verbs = ? where CONTENTID = ?
,但您将 4 个项目附加到 data
。要么您错过了查询中的一列,要么您需要删除'PROCESSED'
。
使用 Oracle,您需要使用 ":name" 而不是 "?"用于 SQL 语句中的绑定占位符。检查 cx_Oracle 文档 Batch Statement Execution and Bulk Loading 和 Using Bind Variables。
【参考方案1】:
这是一个有效的相关示例。该表创建为:
DROP table test_batch_update_python;
CREATE TABLE test_batch_update_python (contentid NUMBER, nouns VARCHAR2(20), verbs VARCHAR2(20));
INSERT INTO test_batch_update_python (contentid) VALUES (1);
INSERT INTO test_batch_update_python (contentid) VALUES (2);
COMMIT;
cursor = connection.cursor()
dataToInsert = [
('shilpa', 'really fast', 1),
('venkat', 'also really fast', 2),
]
print("ALL DATA TO BE UPDATED IN TABLE IS :---> ", dataToInsert)
connection.autocommit = True;
statement = """UPDATE test_batch_update_python SET nouns=:1, verbs=:2 WHERE contentid=:3"""
cursor.setinputsizes(20, 20, None)
cursor.executemany(statement, dataToInsert)
输出是:
ALL DATA TO BE UPDATED IN TABLE IS :---> [('shilpa', 'really fast', 1), ('venkat', 'also really fast', 2)]
然后查询数据给出:
SQL> select * from test_batch_update_python;
CONTENTID NOUNS VERBS
---------- -------------------- --------------------
1 shilpa really fast
2 venkat also really fast
【讨论】:
【参考方案2】:检查此链接以获取更新声明
https://blogs.oracle.com/oraclemagazine/perform-basic-crud-operations-with-cx-oracle-part-3
谢谢
【讨论】:
以上是关于Python:在python中使用executemany()批量上传Oracle表的记录的主要内容,如果未能解决你的问题,请参考以下文章
如何在 Python 3.x 和 Python 2.x 中使用 pip
在 python 中使用没有 python2 和 python1 的 python3 或同时打印? [关闭]