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