0

I am trying read data from one table and write into another table in another database using Python and cx_Oracle.

My script works fine when I read the records one by one but when I fetch more than 100 records using fetchmany, the script fails with the error

cx_Oracle.NotSupportedError: Python value cannot be converted to a database value

This is the script I am trying to run.

src_conn = cx_Oracle.connect(username+'/'+password+'@'+database)
src_cursor=src_conn.cursor()
tgt_conn = cx_Oracle.connect(tgt_username+'/'+tgt_password+'@'+tgt_database)
tgt_cursor=tgt_conn.cursor()
for files in file_path:
    cols=[]
    col_id=[]
    file=files.replace("\n","")
    column_list_query="select COLUMN_NAME,COLUMN_ID from all_tab_columns where owner='GDW' and table_name ='"+file+"' order by column_id"
    col=src_cursor.execute(column_list_query)
    col_list=col.fetchall()

    for value in col_list:
        cols.append(value[0])
        col_id.append(":"+str(value[1]))

    col_names="("+','.join(cols)+")"
    col_ids="("+','.join(col_id)+")"

    insert_statement='INSERT INTO '+file+' '+col_names+' VALUES '+col_ids
    select_statment="SELECT * FROM "+file
    src_cursor.bindarraysize=1000
    src_values=src_cursor.execute(select_statment)
    print("Copy contents into table :"+file)


    def ResultIter(cursor, arraysize=500):
        while True:
            results = src_cursor.fetchmany(arraysize)
            if not results:
                break
            yield results

    tgt_cursor.prepare(insert_statement)
    for result in ResultIter(src_values):

        if not result:
            break
        tgt_cursor.executemany(None,result)
        tgt_conn.commit()
4

1 回答 1

0

该问题已在此处报告,您可以查看那里的评论。

错误的原因是在第一批中,一个或多个被绑定的列总是无,但在随后的一批中,这些列中的一个现在有一个值。这已在 cx_Oracle 源代码中得到纠正,因此您可以自己构建或等待补丁发布。

否则,目前的解决方案如下:

(1) 在一个批次中执行所有插入(但取决于大小,这可能不可用)

(2) 为每个批次创建一个新游标(以便 cx_Oracle 计算每个批次的类型)

(3) 使用 cursor.setinputsizes() 指定类型和大小(可能比较麻烦)

一个演示问题的简单测试用例如下:

import cx_Oracle

conn = cx_Oracle.connect("cx_Oracle/welcome")
cursor = conn.cursor()

cursor.execute("truncate table TestTempTable")

sql = "insert into TestTempTable values (:1, :2)"
cursor.executemany(sql, [(1, None), (2, None)])
cursor.executemany(sql, [(3, None), (4, "Testing")])
于 2018-11-29T23:25:20.487 回答