Search notes:

Python library: python-oracledb

pip install oracledb
#!/usr/bin/env python3

import oracledb
import sys

"""
Connects to an Oracle Database using python-oracledb in Thin mode,
executes a query, and prints the results.
"""

username = '‥'
password = '‥'
dsn = '10.72.68.16:1521/‥'

try:

  # Run in thick mode:
    oracledb.init_oracle_client(lib_dir = '/home/rene/bin/instantclient_23_26')

    connection = oracledb.connect(user=username, password=password, dsn=dsn)
    print("✅ Successfully connected to Oracle Database.")

    with connection.cursor() as cursor:

        cursor.execute('''
            select
               object_name obj,
               object_type typ,
               created     cre
            FROM
               user_objects
            fetch first 5 rows only
       ''')

        rows = cursor.fetchall()
        if not rows:
           print("No data found.")
        else:
           print("\nEmployee Data:")

         # Align the three displayed columns for easier scanning.
           print(f"{'Object Name':<30} {'Object Type':<20} {'Created'}")
           for obj, typ, cre in rows:
               print(f"{obj:<30} {typ:<20} {cre}")

except oracledb.DatabaseError as e:
    error_obj, = e.args
    print(f"❌ Database error: {error_obj.message}", file=sys.stderr)

except Exception as e:
    print(f"❌ Unexpected error: {e}", file=sys.stderr)

finally:
    try:
        if connection:
           connection.close()
           print("🔒 Connection closed.")
    except NameError:
      # connection was never created
        pass

Thin and thick mode

Running python-oracledb in thin mode connects directly to the database, and no further installation is required. This is the default.
Some features are available in thick mode only which require to have Oracle Client libraries (Version 19, 21 or 23) installed.
In order to run python-oracledb in thickmode, oracledb.init_oracle_client() must be execeted.

TODO

Error message DPY-3015: password verifier type 0x939 is not supported by python-oracledb in thin mode, see this link.

See also

cx_Oracle
PEP 249

Index

Fatal error: Uncaught PDOException: SQLSTATE[HY000]: General error: 8 attempt to write a readonly database in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php:78 Stack trace: #0 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(78): PDOStatement->execute(Array) #1 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(30): insert_webrequest_('/notes/developm...', 1788361479, '216.73.217.21', 'Mozilla/5.0 App...', NULL) #2 /home/httpd/vhosts/renenyffenegger.ch/httpsdocs/notes/development/languages/Python/libraries/python-oracledb/index(116): insert_webrequest() #3 {main} thrown in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php on line 78