python database oracle - ghdrako/doc_snipets GitHub Wiki

subprocess.call('sqlldr userid=USERNAME/PASSWORD3@XXX control=XXX.ctl', shell=True)
import os
import subprocess

BASE_DIR = Path(__file__).resolve().parent
control_file = os.path.join(BASE_DIR, 'SAMPLE_ITEM_LOAD.ctrl')
data_file = os.path.join(BASE_DIR, 'sample_item_load.csv')

my_env = os.environ.copy()
my_env["PATH"] = f"{my_env['ORACLE_HOME']}:{my_env['PATH']}"

sql_con = f'{username}/{password}@{host}:{port}/{db_sid}'

sql_ld_command = f'sqlldr {sql_con} CONTROL={control_file} DATA={data_file}'
    sql_ldr_proc = subprocess.Popen(sql_ld_command,shell=True,
                                       env=my_env,
                                       stdin=subprocess.PIPE,
                                       stdout=subprocess.PIPE,
                                       stderr=subprocess.PIPE)
            
    out, err = sql_ldr_proc.communicate()
    retn_code = sql_ldr_proc.wait()