我正在阅读一个包含5列的csv文件并推送到oracle表
我知道这方面有很多资源..但即便如此,我也无法找到解决问题的方法
将CSV读取为python的代码:
import csv
reader = csv.reader(open("sample.csv","r"))
lines=[]
for line in reader:
lines.append(line)
print lines
输出:
[['名字','姓氏','电子邮件','课程名称','状态'] , [' Kristina',' Bohn',' abc@123.com','指南 重症患者管理中的二氧化碳图(CE)', '注册'],' Peggy',' Lutz',' gef@123.com', '在阿片类药物递送期间监测EtCO2的指南(CE)',' In 进展']]
将列表推送到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()
我收到错误:
DatabaseError:ORA-01484:数组只能绑定到PL / SQL语句
请帮我解决问题提前谢谢
答案 0 :(得分:3)
问题是您正在尝试将数组传递给单个insert语句。你有两个选择:
1)使用循环分别插入每一行:
for line in lines:
cursor.execute("insert into ...", line)
2)使用cursor.executemany()来代替数组插入
cursor.executemany("insert into ...", lines)
第二个选项更有效,但您必须确保每行的数据类型一致。如果一行中有一个数字,下一行中有一个字符串,则会引发错误。
答案 1 :(得分:1)
感谢您的回答:) 一旦我改变了,问题就解决了 来自
cur.execute("INSERT INTO TEST_CSODUPLOAD ('FIRSTNAME','LASTNAME','EMAIL','COURSE_NAME','STATUS') VALUES(:1,:2,:3,:4,:5)",lines)
到
cur.executemany("insert into TEST_CSODUPLOAD(Firstname,LastName,email,Course_name,status) values (:1, :2, :3, :4,:5)", lines)
答案 2 :(得分:0)
That error means what it said here
This is not a bug. You are issuing a SQL query, not a PL/SQL statement and this sort of thing is not supported by Oracle. In this particular case you can simply pass the string. If you have multiple strings you will need to use a subquery instead. Look at the table() and cast() operators for inspiration!
You can also dump into a sql file and execute it.