[英]failed to insert data from csv file to oracle using python
I intend to insert data in a csv file row by row into oracle database using Python & Oracle connector, yet for text that are too long, I got an error我打算使用 Python 和 Oracle 连接器将 csv 文件中的数据逐行插入到 oracle 数据库中,但是对于太长的文本,出现错误
ORA-01704: string literal too long
ORA-01704: 字符串文字太长
The code is as follows:代码如下:
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)
I have searched on stackoverflow and tried to set the column name TEST1 to bigger size when the len(row["answer"]) is over 2000 with我在 stackoverflow 上搜索过,并尝试在 len(row["answer"]) 超过 2000 时将列名 TEST1 设置为更大的大小
cur.setinputsizes(TEST1 = cx_Oracle.CLOB)
Yet apparently it does not work since the query form is different.但显然它不起作用,因为查询形式不同。
When running in the else part:在 else 部分运行时:
else:
sqlquery="INSERT INTO TEST VALUES ('%s','%s')" %(row['answer'],today)
cur.setinputsizes(TEST1 = cx_Oracle.CLOB)
cur.execute(sqlquery)
ORA-01036: illegal variable name/number
ORA-01036: 非法变量名/编号
was returned被退回
Also, I have tried to declare the variable to varchar2 to solve this problem, but in vain.另外,我试图将变量声明为 varchar2 来解决这个问题,但徒劳无功。 If anyone has a better idea with this issue, your help would be much much appreciated..
如果有人对这个问题有更好的想法,我们将不胜感激。
First of all, you got ORA-1036 because your sqlquery
just have not any variable.首先,您得到 ORA-1036,因为您的
sqlquery
没有任何变量。 You're trying to set up TEST1 variable but where is it in your code?您正在尝试设置 TEST1 变量,但它在您的代码中的什么位置? With variables your code should look like
INSERT INTO TEST VALUES (:TEST1, :TEST2)
;使用变量,您的代码应该类似于
INSERT INTO TEST VALUES (:TEST1, :TEST2)
; instead of this you're giving nice ability for SQL injection .取而代之的是,您为SQL 注入提供了很好的能力。
About the better idea... As for me, there is no reason to make bad copy of existing decision.关于更好的主意......至于我,没有理由复制现有的决定。 I'd just use Oracle SQL*Loader which can load csv files too.
我只是使用Oracle SQL*Loader ,它也可以加载 csv 文件。
I found this... the cx_Oracle library provide the LOB object to write CLOB object in Oracle.我发现了这个... cx_Oracle 库提供了LOB 对象来在 Oracle 中编写 CLOB 对象。 Read this one , but it was for the 2.X.
阅读这个,但它是为 2.X.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.