Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Oracle DB Connect without Oracle Client

I am trying to build an application in python which will use Oracle Database installed in corporate server and the application which I am developing can be used in any local machine.

Is it possible to connect to oracle DB in Python without installing the oracle client in the local machine where the python application will be stored and executed?

Like in Java, we can use the jdbc thin driver to acheive the same, how it can be achieved in Python.

Any help is appreciated

Installing oracle client, connect is possible through cx_Oracle module. But in systems where the client is not installed, how can we connect to the DB.

like image 502
Bishnu Avatar asked Sep 06 '25 03:09

Bishnu


1 Answers

The python-oracledb driver (that superseded cx_Oracle) does not need Oracle Client libraries. It only takes a few megabytes. Installation is simple:

python -m pip install oracledb

It supports the Python DB API so usage is the same as cx_Oracle, e.g.:

import getpass
import oracledb

un = 'cj'
cs = 'localhost/orclpdb1'
pw = getpass.getpass(f'Enter password for {un}@{cs}: ')

with oracledb.connect(user=un, password=pw, dsn=cs) as connection:
        with connection.cursor() as cursor:        
        sql = """select systimestamp from dual"""
        for (r,) in cursor.execute(sql):
            print(r)

See the python-oracledb documentation and samples.

Also see the python-oracledb release announcement.

The cx_Oracle driver is obsolete and should not be used for any projects.

like image 135
Christopher Jones Avatar answered Sep 07 '25 21:09

Christopher Jones