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

Share on:

python

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

python2

 

3. Install python-oracledb driver:

Use this link for installation: Python for Oracle Linux

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

python3

 

sudo yum -y install python3-oracledb

python4

 

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

Python3

help(‘modules’)

python5

 

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

Share on:

More from this Author

How to Import Custom Visualization in Oracle Analytics Cloud to See Images

In this blog, I will show you how to download and import the Image Gallery Plugin into OCI Oracle Analytics Cloud. using this plugin we can see ... Read More

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 APIs and ... Read More

Back to Top