Oracle Database Python Driver
oracledb is the official Oracle Database adapter for Python, designed to connect Python applications directly to Oracle Database. It is the successor to the popular cx_Oracle driver, providing a modern, high-performance interface that conforms to the Python Database API v2.0 Specification. It features both a 'Thin' mode, which requires no Oracle Client libraries, and an optional 'Thick' mode for advanced functionalities. The library is actively maintained by Oracle, with frequent releases; the current version is 3.4.2.
Warnings
- breaking oracledb is the renamed successor to cx_Oracle. While the API is largely compatible, all new development should use `oracledb` as `cx_Oracle` is now obsolete and no longer actively developed. The primary change is the import statement and the default 'Thin mode'.
- gotcha oracledb operates in two modes: 'Thin' (default, pure Python, no client libraries needed) and 'Thick' (requires Oracle Client libraries for extended features). You can only enable 'Thick mode' once per process by calling `oracledb.init_oracle_client()` *before* creating any connections or connection pools. Attempting to call it after connections are established will result in an error.
- deprecated The `oracledb.makedsn()` method is deprecated. Modern code should construct connection strings directly using keyword arguments in `oracledb.connect()` or `oracledb.create_pool()`, or use an Easy Connect string.
- gotcha In 'Thin mode', `oracledb` does not support Oracle Database native network encryption or checksumming. If these are required, 'Thick mode' must be used, or TLS encryption can be enabled as an alternative.
- gotcha When connecting to an Oracle Autonomous Database (ADB) in 'Thin mode' using a wallet, the `wallet_password` parameter in the connection string or `oracledb.connect()` is *not* your database user password. It is the password used to encrypt the ADB wallet itself, set when downloading the wallet from the Oracle Cloud Console.
Install
-
pip install oracledb
Imports
- oracledb
import oracledb
- connect
oracledb.connect(...)
Quickstart
import os
import oracledb
# Database credentials from environment variables for security
USERNAME = os.environ.get('ORACLE_DB_USER', 'your_username')
PASSWORD = os.environ.get('ORACLE_DB_PASSWORD', 'your_password')
CONNECT_STRING = os.environ.get('ORACLE_DB_DSN', 'localhost/orclpdb') # e.g., 'hostname:port/service_name'
try:
# Establish a connection in Thin mode (default)
with oracledb.connect(user=USERNAME, password=PASSWORD, dsn=CONNECT_STRING) as connection:
print(f"Successfully connected to Oracle Database: {connection.version}")
with connection.cursor() as cursor:
# Execute a simple query
cursor.execute("SELECT SYSDATE FROM DUAL")
for row in cursor:
print(f"Current database date: {row[0]}")
except oracledb.Error as e:
error_obj, = e.args
print(f"Error connecting to Oracle Database: {error_obj.message}")
print("Please ensure ORACLE_DB_USER, ORACLE_DB_PASSWORD, and ORACLE_DB_DSN environment variables are set correctly,")
print("or replace the placeholder values in the script.")