If you want to connect Oracle database from Python, then you need to install cx_Oracle package. In this tutorial, I am giving steps on how to download and install cx_Oracle package for Python on Windows.
Software Version Used
- Python 3.7
- cx_Oracle 6.4
- OS Windows 10
- Oracle Database 11g
Steps to Download and Install cx_Oracle Package for Python on Windows
- Click on the Download cx_Oracle link to download the package from Github.
- Extract the zip file to a folder on Windows. For example, F:\cx_oracle.
- Now open the command prompt and change the current directory to the F:\cx_oracle directory to install cx_Oracle package.
- Then run the following command.
python -m pip install cx_Oracle --upgrade pip
It will install the cx_Oracle package for Python on Windows, and you will get the messages as shown below.
Collecting pip Downloading https://files.pythonhosted.org/packages/5f/25/e52d3f31441505a5f3af41213346e5b6c221c9e086a166f3703d2ddaf940/pip-18.0-py2.py3-none-any.whl (1.3MB) 100% |¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦| 1.3MB 2.5MB/s Installing collected packages: pip Found existing installation: pip 10.0.1 Uninstalling pip-10.0.1: Successfully uninstalled pip-10.0.1 Successfully installed pip-18.0
Now Test The Connection To Oracle From Python
Open Python IDE and give the following commands one by one to test the connection. Change your_username and your_psw to your database username and password.
-
import cx_Oracle
-
connection = cx_Oracle.connect("your_username", "your_psw", "localhost/orcl")
-
cursor = connection.cursor()
-
cursor.execute("""select to_char(sysdate, 'dd-mon-yyyy') from dual""")
-
for cdate in cursor:
-
print("Today the date is ", cdate)
Output
Today the date is ('05-sep-2018',)