I am struggling with below code to twik where i am going wrong and why my table is not getting loaded with data
My Oracle table structure :
Table name : test
create table test
(
id number,
name varchar2(50 char),
surname varchar2(50 char),
age number,
country varchar2(50 char)
)
I am not getting any error in log file and nor table loaded with data
I have 10,000 records in file and records are pipe separated : '|'
My code:
import logging
import cx_Oracle
import csv
insertQuery="insert into test (id,name,surname,age,country) values (:1, :2 , :3 , :4 , :5)"
invalue='line[0], line[1] , line[2] , line[3] , line[4]'
oracleconnection = 'user/password@hostIP/database'
my_separator='|'
file_name = demo_2021.csv
def loadtable(oracleconnection,my_query,file_name,my_separator):
try:
con=cx_Oracle.connect(oracleconnection)
cur=con.cursor()
# Predefine the memory areas to match the table definition
cur.setinputsizes(None, 25)
# Adjust the batch size to meet your memory and performance requirements
batch_size = 10000
with open(file_name, 'r') as csv_file:
csv_reader = csv.reader(csv_file, delimiter=my_separator,header=None)
sql = insertQuery
data = []
for line in csv_reader:
data.append((invalue))
if len(data) % batch_size == 0:
cur.executemany(sql, data)
data = []
if data:
cur.executemany(sql, data)
con.commit()
except Exception as er:
logging.error(f'{er}')
data.append((invalue))withdata.append(line)?data.append(tuple(line)). It needs a list of tuples...data.append((invalue))makes no sense, because for every line you append a tuple with the same content: exactly one string declared at the very beginning. But your query expects five bind variables for each line. Iflineis a tuple, then append it as is and thenexecutemany. See cx_Oracle: Batch Statement Execution and Bulk Loading