0
  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() )

4

2 回答 2

0

检查此链接以获取和更新声明

https://blogs.oracle.com/oraclemagazine/perform-b​​asic-crud-operations-with-cx-oracle-part-3

谢谢

于 2020-07-04T01:10:09.450 回答
0

这是一个有效的相关示例。该表创建为:

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
于 2020-07-03T03:08:19.467 回答