SAP HANA Python Client (hdbcli)

2.28.19 · active · verified Fri Apr 10

hdbcli is the official SAP HANA Python Client, implementing the Python Database API Specification v2.0 (PEP 249). It enables Python applications to connect to SAP HANA databases, execute SQL statements, and manage data. The library is actively maintained by SAP, with the current version 2.28.19 released on March 27, 2026, and typically sees regular updates.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to establish a connection to an SAP HANA database using `hdbcli`, execute a simple `SELECT` statement, create a table, insert data, and fetch results. It uses environment variables for secure credential management. Remember to set `HANA_HOST`, `HANA_PORT`, `HANA_USER`, and `HANA_PASSWORD` in your environment.

import os
from hdbcli import dbapi

# Retrieve credentials from environment variables for security
host = os.environ.get('HANA_HOST', 'your_hana_host.com')
port = os.environ.get('HANA_PORT', '30015') # Example for single-tenant, adjust as needed
user = os.environ.get('HANA_USER', 'YOUR_USER')
password = os.environ.get('HANA_PASSWORD', 'YOUR_PASSWORD')

try:
    # Establish a connection to the SAP HANA database
    # For HANA Cloud, port is typically 443 with encryption=True (default).
    # For on-premise, adjust port (e.g., 3NN13/3NN15) and encryption.
    conn = dbapi.connect(address=host, port=int(port), user=user, password=password)
    print(f"Successfully connected to HANA at {host}:{port}")

    # Create a cursor object
    cursor = conn.cursor()

    # Execute a simple query
    cursor.execute("SELECT CURRENT_UTCTIMESTAMP FROM DUMMY")

    # Fetch the result
    result = cursor.fetchone()
    print(f"Current UTC Timestamp from HANA: {result[0]}")

    # Execute an update/insert (example: creating a table and inserting data)
    try:
        cursor.execute("DROP TABLE MY_TEST_TABLE_HDBCLI")
        print("Dropped existing MY_TEST_TABLE_HDBCLI.")
    except dbapi.Error as e:
        if "Cannot drop table" not in str(e): # Ignore if table doesn't exist
            print(f"Error dropping table: {e}")

    cursor.execute("CREATE TABLE MY_TEST_TABLE_HDBCLI (ID INTEGER PRIMARY KEY, NAME VARCHAR(255))")
    print("Created MY_TEST_TABLE_HDBCLI.")

    sql_insert = "INSERT INTO MY_TEST_TABLE_HDBCLI (ID, NAME) VALUES (?, ?)"
    cursor.execute(sql_insert, (1, 'Item One'))
    cursor.execute(sql_insert, (2, 'Item Two'))
    print("Inserted two rows.")

    # Fetch inserted data
    cursor.execute("SELECT * FROM MY_TEST_TABLE_HDBCLI")
    rows = cursor.fetchall()
    print("Data in MY_TEST_TABLE_HDBCLI:")
    for row in rows:
        print(row)

    # Commit the transaction (if autocommit is off, which it isn't by default in hdbcli)
    # conn.commit() # Not strictly necessary if autocommit is on (default in hdbcli)

except dbapi.Error as e:
    print(f"HANA Database error: {e}")
except Exception as e:
    print(f"An unexpected error occurred: {e}")
finally:
    # Close the cursor and connection
    if 'cursor' in locals() and cursor:
        cursor.close()
    if 'conn' in locals() and conn:
        conn.close()
        print("Connection closed.")

view raw JSON →