Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
cx_Oracle.NotSupportedError:无法将Python值转换为数据库值_Python_Python 3.x_Cx Oracle_Executemany - Fatal编程技术网

cx_Oracle.NotSupportedError:无法将Python值转换为数据库值

cx_Oracle.NotSupportedError:无法将Python值转换为数据库值,python,python-3.x,cx-oracle,executemany,Python,Python 3.x,Cx Oracle,Executemany,我正在尝试使用Python和cx_Oracle从一个表中读取数据并写入另一个数据库中的另一个表 当我逐个读取记录时,我的脚本工作正常,但当我使用fetchmany获取超过100条记录时,脚本失败并出现错误 cx_Oracle.NotSupportedError: Python value cannot be converted to a database value 这是我正在尝试运行的脚本 src_conn = cx_Oracle.connect(username+'/'+password+

我正在尝试使用Python和cx_Oracle从一个表中读取数据并写入另一个数据库中的另一个表

当我逐个读取记录时,我的脚本工作正常,但当我使用
fetchmany
获取超过100条记录时,脚本失败并出现错误

cx_Oracle.NotSupportedError: Python value cannot be converted to a database value
这是我正在尝试运行的脚本

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()

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

错误的原因是,在第一批中,一个或多个被绑定的列始终为“无”,但在随后的批中,这些列中的一个现在具有值。这已在cx_Oracle源代码中更正,因此您可以自行构建,也可以等待发布补丁

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

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")])
(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")])

如果您正在对同一个数据库进行读写操作,那么您几乎肯定希望使用SQL或PL/SQL来执行此操作,这样数据就不必通过网络返回到数据库中。我正在从一个数据库复制到另一个数据库。当我使用sqlplus或toad时,我发现某些数据集存在问题,并发现我可以使用此脚本克服这些问题。您能否发布发生此故障时的结果?我试图复制您的问题,但到目前为止还无法复制。