Oracle
 sql >> Teknologi Basis Data >  >> RDS >> Oracle

Tidak dapat memuat data dari file ke tabel Oracle menggunakan executeMany()

Dengan file data pengujian ini:

1|Fred|Nurke|21|UK
2|Henry|Crun|21|UK

kode ini berfungsi:

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)

Dalam SQL*Plus setelahnya:

  ID NAME  SURNAME AGE COUNTRY
---- ----- ------- --- -------
   1 Fred  Nurke    21 UK
   2 Henry Crun     21 UK



  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. Keluaran skrip Pengembang SQL memotong lebar sys_refcursor

  2. Membuat penangan logging untuk terhubung ke Oracle?

  3. Kinerja:rank() vs sub-kueri. Sub permintaan memiliki biaya lebih rendah?

  4. Cara menghapus baris di oracle

  5. PILIH Data DARI KURSOR PAKET, cetak