Eclipsys Blog

How to Connect Python Application to OCI DBCS using Python-Oracledb – Eclipsys

Written by Manoj Kumar | Oct 17, 2023 3:00:00 PM

Pre-requisite: You should have an Oracle Database running on DBCS OCI

 

What is Python-oracledb?

The python-oracledb driver is a Python programming language extension module allowing Python programs to connect to the Oracle Database. Python-oracledb is the new name for Oracle’s popular cx_Oracle driver.

 

Python-oracledb Architecture:

The python-oracledb driver enables access to the Oracle Database using either one of two modes. By default, python-oracledb runs in a “thin” mode, which connects directly to the Oracle Database. This mode does not need Oracle Client libraries.

However, some additional features are available when python-oracledb uses them. Python-oracledb applications that load the Oracle Client libraries via an application script runtime option are said to be in “thick” mode.

The database can be on the same machine as Python or it can be remote.

1. Create DBCS in OCI. (as a pre-requisite)

2. Install Python3:

sudo yum install -y python3

I already have installed, it to check the version

 

3. Install python-oracledb driver:

Use this link for installation: Python for Oracle Linux

sudo yum -y install Oracle Linux-developer-release-el7

 

sudo yum -y install python3-oracledb

 

4. Test your installation by launching the Python console and listing the available modules. Where you can see the oracledb module.

Python3

help(‘modules’)

 

5. Create a small Python application script that connects with DBCS PDB.

import oracledb
import os

oracledb.init_oracle_client()

db_user = os.environ.get('DBAAS_USER_NAME', 'SYSTEM')
db_password = os.environ.get('DBAAS_USER_PASSWORD', '********************')
db_connect = os.environ.get('DBAAS_DEFAULT_CONNECT_DESCRIPTOR', "10.10.10.10:1521/fsuat")

connection = oracledb.connect(user=db_user, password=db_password, dsn=db_connect)
cursor = connection.cursor()
sql = """select name from v$database"""
for r in cursor.execute(sql):
print(r)

The above script used oracledb.init.oracle_client() which means thick mode. As thick mode used Oracle client libraries.

I used thick mode because DBCS DB uses encryption and encryption is only supported by thick mode.

If you are not using encryption you can use thin mode without Oracle client libraries.

The below link shows Oracle Database features supported by python-oracledb Thin and Thick modes:

Features supported by python-oracledb