Creating a Python script to extract results to CSV from an Oracle Database

Hi All,

Maybe one day you guys will need to extract results from a Database and export to CSV. And the best way to do this is using a simple script using Python, a simple and elegant language.

In my case, I needed simple stuff. Just extract the results from an Oracle Database in a CSV format. The output would be a report for the Business areas. Here are the steps I did in order to achieve the expected result.

First of all you need to install latest Python version

Now you need to to add the line

in your visudo.

and below the section

add the line

 

Make sure that you have the ORACLE_HOME variable set. Don’t forget that.

Now you need to install Python PIP

and we can now install the Oracle_CX library

All set. Below is the code as I used to start developing the script. It connects to a DEMO DB (which comes with Oracle 12c).

You can use it as a starting point for your needs.

And you can check on my GitHub also:

https://github.com/andrealmar/csv_report_script/blob/master/csv_report.py

See you next time!