Skip to main content
 首页 » 编程设计

python之使用python从CSV插入到oracle表中

2025年05月04日15lovecherry

我正在读取一个包含 5 列的 csv 文件并推送到 oracle 表

CSV file Structure

我知道这方面有很多资源..但即便如此我还是找不到解决我问题的方法

将 CSV 文件读取到 python 的代码:

 import csv 
 reader = csv.reader(open("sample.csv","r")) 
 lines=[] 
 for line in reader: 
      lines.append(line) 
  print lines  

输出:

[['Firstname', 'LastName', 'email', 'Course_name', 'status'], ['Kristina', 'Bohn', 'abc@123.com', 'Guide to Capnography in the Management of the Critically Ill Patient (CE)', 'Registered'], ['Peggy', 'Lutz', 'gef@123.com', 'Guide to Monitoring EtCO2 During Opioid Delivery (CE)', 'In Progress']]

将列表推送到 Oracle 表的代码:

import cx_Oracle 
con = cx_Oracle.connect('username/password@tamans*****vd/Servicename') 
ver=con.version.split(".") 
print(ver) 
cur=con.cursor() 
cur.execute("INSERT INTO TEST_CSODUPLOAD ('FIRSTNAME','LASTNAME','EMAIL','COURSE_NAME','STATUS') VALUES(:1,:2,:3,:4,:5)",lines) 
con.commit () 
cur.close() 

我收到错误:

数据库错误:ORA-01484:数组只能绑定(bind)到 PL/SQL 语句

请帮我解决这个问题提前致谢

请您参考如下方法:

问题是您正试图将数组传递给单个插入语句。您在这里有两个选择:

1) 使用循环分别插入每一行:

for line in lines: 
    cursor.execute("insert into ...", line) 

2) 使用 cursor.executemany() 而不是进行数组插入

cursor.executemany("insert into ...", lines) 

第二个选项更有效,但您必须确保每一行的数据类型是一致的。如果一行中有一个数字而下一行中有一个字符串,则会引发错误。

documentation给出了更多细节。