简体   繁体   English

无法使用python将数据从csv文件插入oracle

[英]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.

相关问题 使用python从CSV插入oracle表 - Insert into oracle table from CSV using python 使用python将数据从csv文件插入到oracle时出现错误ORA-01722:无效编号 - while inserting data to oracle from csv file using python getting error ORA-01722: invalid number 如何有效地使用 Python 将 CSV 文件数据插入到 MYSQL 中? - How to insert a CSV file data into MYSQL using Python efficiently? 从 sql 文件或使用 csv 文件将数据插入 mysql - Insert data into mysql from sql file or using csv file 使用python从csv文件中累积数据 - data accumulating from csv file using python 使用 CSV 和文本文件中的 python 插入 SQL 服务器表 - insert into SQL Server table using python from CSV and Text file 如何使用 pandas 将附加列表中的数据插入 csv 文件? - How to insert data from a appened list to the csv file using pandas? 使用 python 或 Matlab 将数据从 csv 文件转换为 csv - Transposing the data from csv file to csv using python or Matlab 获取 Python 代码的帮助,该代码从一个 CSV 文件中获取数据并将其插入到另一个 CSV 文件中 - Get help for Python code that get data from one CSV file and insert it into another CSV file 使用 Python 将 JSON 数据插入 Oracle - Insert JSON data into Oracle using Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM