Skip to Content

Connect to SAP HANA Using Python

Install SAP HANA client drivers on Python and connect to an SAP HANA instance.
You will learn
  • How to install the SAP HANA client driver for Python
  • How to install the client driver using a virtual environment (optional)
  • How to connect to an instance of SAP HANA
  • How to use environment variables to provide connection parameters

Prerequisites

  • A running SAP HANA database instance and credentials of a database user.
  • Familiarity with Python.

We recommend using virtual environments to localize your Python runtime environment. If you choose to use virtual environments, you can find a guide on using virtual environments here.

  • Step 1

    Create a virtual environment to localize dependencies:

    Shell
    Copy
    python -m venv venv
    

    Activate the virtual environment:

    Shell
    Copy
    source ./venv/bin/activate
    

    After activating the virtual environment, you should see (venv) in front of your Linux user in your bash shell. For example:

    source_venv

    If you’re on Windows:

    cmd
    Copy
    .\venv\Scripts\activate
    
    source_venv_windows

    Create a dependency file and name it requirements.txt:

    requirements.txt
    Copy
    hdbcli
    ### add other dependencies below ###
    

    Install the dependencies:

    Shell
    Copy
    pip install -r requirements.txt
    
    install_from_requirements

    The dependencies will be saved in your venv directory.

    You should see the following after installing the client:

    Shell
    Copy
    Installing collected packages: hdbcli
    Successfully installed hdbcli-2.x.xxx
    
  • Step 2

    To establish a simple connection to SAP HANA, you need the following information:

    • Database host
    • Database port
    • Database user
    • Password
    Python
    Copy
    #connect.py
    
    #Import your dependencies
    from hdbcli import dbapi
    #Initialize your connection
    conn = dbapi.connect(
        address='<Database host>',
        port='<Database port>',
        user='<Database user>',
        password='<Password>'
    )
    #If no errors, print connected
    print('connected')
    
    ### rest of your code here ###
    
    

    You should never have database information directly in your code; you are only doing this for a connection test.

    Run the file with:

    Shell
    Copy
    python3 connect.py
    

    If the connection information used is correct, you should see the following printed in your shell:

    Shell
    Copy
    connected
    
  • Step 3

    Now that you’ve successfully connected to the database, we need to take out connection parameters and credentials out of the source code. Having connection parameters directly in your source code is insecure, one option is to provide these parameters at runtime as environment variables instead.

    Export your environment variables to your bash profile through your preferred method.

    Bash
    Copy
    export HOST="<Database Host>"
    export PORT="<Database Port>"
    export DB_USER="<Database User>"
    export DB_PW="<Password>"
    

    We’ll use the os package in Python to access these environment variables to connect.

    Python
    Copy
    from hdbcli import dbapi
    import os
    
    conn = dbapi.connect(
        address=os.environ['HOST'],
        port=os.environ['PORT'],
        user=os.environ['DB_USER'],
        password=os.environ['DB_PW']
    )
    
    print('connected')
    
    #test select from dummy table
    cursor = conn.cursor()
    cursor.execute("SELECT * FROM DUMMY")
    for result in cursor:
        print(result)
    
    

    Test run the Python file again with:

    Shell
    Copy
    python connect.py
    

    Enter console output below to validate your connection.

Back to top