【发布时间】:2018-05-10 09:42:45
【问题描述】:
我打算使用 Python 和 Oracle 连接器将 csv 文件中的数据逐行插入到 oracle 数据库中, 但是对于太长的文本,我得到了一个错误
ORA-01704: 字符串文字太长
代码如下:
from time import gmtime, strftime
with open(path-to-csv-file) as f:
reader=csv.DictReader(f,delimiter=',')
for row in reader:
today = strftime("%Y-%m-%d %H:%M:%S", gmtime())
if len(row['answer']) < 2000:
###[ The query in this part works]###
sqlquery="INSERT INTO TEST VALUES ('%s','%s')" %(row['answer'],today)
cur.execute(sqlquery)
else:
###[ The query below got and error ORA-01704: string literal too long]###
sqlquery="INSERT INTO TEST VALUES ('%s','%s')" %(row['answer'],today)
cur.execute(sqlquery)
我在 stackoverflow 上搜索并尝试在 len(row["answer"]) 超过 2000 时将列名 TEST1 设置为更大的大小 与
cur.setinputsizes(TEST1 = cx_Oracle.CLOB)
但显然它不起作用,因为查询形式不同。
在else部分运行时:
else:
sqlquery="INSERT INTO TEST VALUES ('%s','%s')" %(row['answer'],today)
cur.setinputsizes(TEST1 = cx_Oracle.CLOB)
cur.execute(sqlquery)
ORA-01036: 非法变量名/编号
被退回
另外,我试图将变量声明为 varchar2 来解决这个问题,但没有成功。 如果有人对此问题有更好的想法,我们将非常感谢您的帮助..
【问题讨论】: