How to export a table to csv or excel format
I need to export a oracle table to a csv/excel file 开发者_运维问答format (along with the column headings). A solution through cx_oracle or through sqlplus welcome.
Python code from the comment:
con = cx.connect()
cur = con.cursor()
printer = cur.execute(sqlcode)
con.commit()
perhaps use csv module (from standard library):
import csv
cursor = connection.cursor() # assuming you know how to connect to your oracle db
cursor.execute('select * from table_you_want_to_turn_to_csv')
with open('output_file.csv', 'wb') as fout:
writer = csv.writer(fout)
writer.writerow([ i[0] for i in cursor.description ]) # heading row
writer.writerows(cursor.fetchall())
If you have loads of data, unroll the fetchall() into a loop.
Happy trails!
to create XLS file, use xlwt module from the python-excel project.
For a non-python solution, Tom Kyte has some excellent scripts for generating CSV files using PL/SQL (UTL_FILE), SQL*Plus, and Pro*C.
精彩评论