pyexasol Exasol Python Driver

2.1.0 · active · verified Sun Mar 29

pyexasol is the officially supported Python connector for Exasol, designed for high-performance data handling with low overhead, fast HTTP transport, and compression. It provides an API for parallel data stream processing, offering significant performance improvements over ODBC/JDBC solutions, especially with `pandas`, `parquet`, and `polars`. The library is actively maintained with a regular release cadence, often seeing monthly or bi-monthly updates.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to establish a connection to an Exasol database, execute DDL and DML statements, and fetch results. It highlights the use of `ExaConnection` and basic SQL operations. For local testing without valid SSL certificates, you might need to adjust `websocket_sslopt` as noted in the code comments. Credentials should ideally be managed via environment variables or a secure configuration.

import os
from pyexasol import ExaConnection

# Replace with your Exasol connection details or environment variables
EXASOL_HOST = os.environ.get('EXASOL_HOST', '127.0.0.1')
EXASOL_PORT = os.environ.get('EXASOL_PORT', '8563')
EXASOL_USER = os.environ.get('EXASOL_USER', 'sys')
EXASOL_PASSWORD = os.environ.get('EXASOL_PASSWORD', 'exasol')

try:
    # Connect to the Exasol database
    # For production, ensure proper SSL options and fingerprint are used
    # For local/testing without certs, add: websocket_sslopt={'check_hostname': False, 'verify_mode': 0}
    # See warnings for v1.0.0 regarding strict certificate verification defaults.
    con = ExaConnection(
        dsn=f"{EXASOL_HOST}:{EXASOL_PORT}",
        user=EXASOL_USER,
        password=EXASOL_PASSWORD
    )
    print("Successfully connected to Exasol.")

    # Execute a DDL statement
    con.execute("CREATE SCHEMA IF NOT EXISTS MY_SCHEMA")
    con.execute("OPEN SCHEMA MY_SCHEMA")

    # Execute a DML statement
    con.execute("CREATE OR REPLACE TABLE my_table (id INT, name VARCHAR(100))")
    con.execute("INSERT INTO my_table VALUES (1, 'Alice'), (2, 'Bob')")

    # Fetch data
    stmt = con.execute("SELECT * FROM my_table ORDER BY id")
    results = stmt.fetchall()
    print("Fetched results:", results)

    # Fetch data into a pandas DataFrame (requires `pyexasol[pandas]`)
    # import pandas as pd
    # df = con.export_to_pandas("SELECT * FROM my_table")
    # print("Fetched into DataFrame:\n", df)

except Exception as e:
    print(f"An error occurred: {e}")
finally:
    if 'con' in locals() and con.is_connected():
        con.close()
        print("Connection closed.")

view raw JSON →