How to connect Python application to OCI DBCS using Python-oracledb

In this blog I will show you how to connect Python application to OCI DBCS using the python-Oracledb thick mode.




Pre-requisite: You should have 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 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 Oracle Database using either one of two modes. By default, python-oracledb runs in a "thin" mode, which connects directly to 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, to check the version


3. Install python-oracledb driver:

Use this link for installation: Python for Oracle Linux

sudo yum -y install oraclelinux-developer-release-el7

sudo yum -y install python3-oracledb


4. Test your install by launching the Python console and list 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 use encryption and encryption is only supported by thick mode.
If you are not using encryption you can use thin mode without oracle client libraries.
Below link shows Oracle Database features supported by python-oracledb Thin and Thick modes:
Features supported by python-oracledb

No comments:

Post a Comment

Analyze Invoices with Oracle Analytics and AI Document Understanding

OCI Document Understanding is an AI service that enables developers to extract text, tables, and other key data from document files through...