I want to connect oracle database to python and using select statement whatever result i will get, I want that result to be exported as csv file in sftp location.
I know we can connect oracle with python using cx_oracle package.
But my concern is to get data from oracle to csv and export that.
Also to make a note my data is bigger in size.
Can anyone help me to get the solution which could be fast as well.
Thanks in advance.
Start with something like this, and change to meet your requirements:
import csv
import os
import getpassimport oracledbun = os.environ.get('PYTHON_USERNAME')
cs = os.environ.get('PYTHON_CONNECTSTRING')
pw = getpass.getpass(f'Enter password for {un}@{cs}: ')connection = oracledb.connect(user=un, password=pw, dsn=cs)sql = 'select * from employees'with connection.cursor() as cursor:cursor.arraysize = 5000with open("output.csv", "w", encoding='utf-8') as outputfile:writer = csv.writer(outputfile, lineterminator="\n")results = cursor.execute(sql)writer.writerows(results)
In particular, you will want to tune the arraysize
value.
See Tuning python-oracledb.