Skip to content

Instantly share code, notes, and snippets.

@mindshoot
Last active January 20, 2022 17:03
Show Gist options
  • Save mindshoot/0d6e3d84ff05faa13f4d25e3a7993e1f to your computer and use it in GitHub Desktop.
Save mindshoot/0d6e3d84ff05faa13f4d25e3a7993e1f to your computer and use it in GitHub Desktop.
Connecting to Oracle in SQLAlchemy when all you have is a cx_Oracle connection string

SQLAlchemy supports creating an engine to wrap a cx_Oracle connection, without having to attempt to translate the connection string into a form supported by SQLAlchemy. This is good for normal SQLAlchemy code and also for using it to pull data using pandas. Here it is in use:

import pandas as pd
from sqlalchemy import create_engine
import cx_Oracle

connection_string = 'user/pass@//host:port/name'
conn_factory = lambda: cx_Oracle.connect(connection_string)

engine = create_engine(
    "oracle://", 
    creator=conn_factory
)

pd.read_sql("select sys_context('userenv', 'current_schema') as schema from dual", engine)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment