Search code examples
pythonsqlalchemycx-oraclepython-oracledb

Using SQLAlchemy to connect to OracleDB using python-oracledb instead of cx_Oracle


I am trying to create an engine using the python-oracledb library instead of cx_Oracle but it shows cx_Oracle module not found. Is there any way I can make sqlalchemy create an engine using python-oracledb?

I used to create an engine with the following if I was using cx_Oracle:

conn_str = "oracle+cx_oracle://{user}:{password}@{host}:{port}"

I am using the below code to make it use python-oracledb.

import oracledb
from sqlalchemy import create_engine, text

oracledb.init_oracle_client()

engine = create_engine(f'oracle://{p_username}:{p_password}@{p_dns}:{p_port}', max_identifier_length=128)

print(engine)

Solution

  • oracle://… is equivalent to oracle+cx_oracle://…. As mentioned in the documentation, if you want to use python-oracledb you need to specify oracle+oracledb://…