cx_Oracle 使用 CLOB 执行many
我正在尝试解析多个 CSV 并使用 cx_Oracle 将它们的数据插入到表中。我使用execute 插入到表中没有问题,但是当我尝试使用executemany 执行相同的过程时,出现错误。我的代码使用有效的执行,
with open(key,'r') as file:
for line in file:
data = data.split(",")
query = "INSERT INTO " + tables[key] + " VALUES ("
for col in range(len(data)):
query += ":" + str(col) + ","
query = query[:-1] + ")"
cursor.execute(query, data)
但是当我将其替换为时
with open(key,'r') as file:
list = []
for line in file:
data = data.split(",")
list.append(data)
if len(list) > 0:
query = "INSERT INTO " + tables[key] + " VALUES ("
for col in range(len(data)):
query += ":" + str(col) + ","
query = query[:-1] + ")"
cursor.prepare(query)
cursor.executemany(None,list)
,在尝试插入具有 CLOB 列且数据超过 4000 字节的表时,出现“ValueError:字符串数据太大”。当表没有 CLOB 列时,Executemany 效果很好。有没有办法告诉cx_Oracle 在执行many 时将适当的列视为CLOB?
I am attempting to parse multiple CSVs and insert their data into tables using cx_Oracle. I have no problem inserting into the tables using execute but when I try the same procedure with executemany I get an error. My code using execute that works is
with open(key,'r') as file:
for line in file:
data = data.split(",")
query = "INSERT INTO " + tables[key] + " VALUES ("
for col in range(len(data)):
query += ":" + str(col) + ","
query = query[:-1] + ")"
cursor.execute(query, data)
but when I replace it with
with open(key,'r') as file:
list = []
for line in file:
data = data.split(",")
list.append(data)
if len(list) > 0:
query = "INSERT INTO " + tables[key] + " VALUES ("
for col in range(len(data)):
query += ":" + str(col) + ","
query = query[:-1] + ")"
cursor.prepare(query)
cursor.executemany(None,list)
I get "ValueError: string data too large" when trying to insert into a table that has CLOB columns and the data is over 4000 bytes. Executemany works great when the table doesn't have a CLOB column. Is there a way I can tell cx_Oracle to treat the appropriate columns as CLOBs when it does executemany?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试将大列的输入大小设置为
cx_Oracle.CLOB
。如果您有二进制数据,则可能不起作用,但应该适用于CSV
中的任何文本。2K
值可能低于其需要的值。请注意,当涉及
CLOB
列时,executemany
似乎要慢很多,但仍然比重复执行要好:Try setting the input size for the large columns to
cx_Oracle.CLOB
. Might not work if you have binary data, but should work for any text you have in aCSV
. The2K
value is probably lower than it needs to be.Note that
executemany
seems to be a lot slower when there areCLOB
columns involved, but still better than repeated executes: