Avec ce fichier de données de test :
1|Fred|Nurke|21|UK
2|Henry|Crun|21|UK
ce code fonctionne :
import logging
import cx_Oracle
import csv
import sys
import os
if sys.platform.startswith("darwin"):
cx_Oracle.init_oracle_client(lib_dir=os.environ.get("HOME")+"/Downloads/instantclient_19_8")
insertQuery="insert into test (id,name,surname,age,country) values (:1, :2, :3, :4, :5)"
oracleconnection = 'un/[email protected]/orclpdb1'
my_separator='|'
file_name = 'demo_2021.csv'
try:
con=cx_Oracle.connect(oracleconnection)
cur=con.cursor()
# Predefine the memory areas to match the table definition
cur.setinputsizes(None, 50, 50, None, 50)
# 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)
data = []
for line in csv_reader:
data.append((line[0], line[1], line[2], line[3], line[4]))
if len(data) % batch_size == 0:
cur.executemany(sql, data)
data = []
if data:
cur.executemany(insertQuery, data)
con.commit()
except Exception as er:
print(er)
Dans SQL*Plus ensuite :
ID NAME SURNAME AGE COUNTRY
---- ----- ------- --- -------
1 Fred Nurke 21 UK
2 Henry Crun 21 UK