Slide 16
Slide 16 text
Loading CSV files
import cx_Oracle, csv
batch_size = 10000 # Adjust to your memory and performance requirements
connection = cx_Oracle.connect(user="scott", password=pw, dsn="localhost/orclpdb1")
with connection.cursor() as cursor:
sql = "insert into t (k, first_name, last_name, country) values (:1, :2, :3, :4)"
cursor.setinputsizes(None, 30, 30, 30) # predefine memory sizes to hold the input records
with open("csv/data1.csv", "r") as csv_file:
csv_reader = csv.reader(csv_file, delimiter=',')
data = []
for line in csv_reader:
data.append((line[0], line[1], line[2], line[3])) # e.g [('1', 'Fred', 'Nurke', 'UK')]
if len(data) % batch_size == 0:
cursor.executemany(sql, data) # call executemany() per batch - don’t call execute() per record!
data = []
if data:
cursor.executemany(sql, data)
connection.commit()
cx_Oracle - Python DB API for Oracle Database
3/28/2022
Copyright © 2021, Oracle and/or its affiliates | Confidential: Internal/Restricted/Highly Restricted
16