How to connect Oracle database in Python
In this article, we will learn how to connect oracle database in python.
Step 1: First we need to install
cx_Oracle, it is a module that enables access to Oracle Database and conforms to the Python database API specification.
python -m pip install cx_Oracle --upgrade
Here we are making a Standalone connection. The standalone connections are useful when the application has a single user session to the Oracle database.
Next create a module
config.py to store the Oracle database’s configuration:
username = 'OT' password = '<password>' dsn = 'localhost/pdborcl' port = 1512 encoding = 'UTF-8'
dsn has two parts the server (
localhost) and the pluggable database (
To create a standalone connection, you use the
cx_Oracle.connect() method or
connect.py shows how to create a new connection to Oracle Database:
import cx_Oracle import config connection = None try: connection = cx_Oracle.connect( config.username, config.password, config.dsn, encoding=config.encoding) # show the version of the Oracle Database print(connection.version) except cx_Oracle.Error as error: print(error) finally: # release the connection if connection: connection.close()