Skip to Content

Connect to SAP HANA Using Python

test
0 %
Connect to SAP HANA Using Python
Details
// Explore More Tutorials

Connect to SAP HANA Using Python

08/22/2019

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

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: Install the hdbcli dependency

Create a virtual environment to localize dependencies:

python -m venv venv

Activate the virtual environment:

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:

.\venv\Scripts\activate
source_venv_windows

Create a dependency file and name it requirements.txt:

hdbcli
### add other dependencies below ###

Install the dependencies:

pip install -r requirements.txt
install_from_requirements

The dependencies will be saved in your venv directory.

Install the hdbcli package:

pip install hdbcli

Install the hdbcli package:

pip install hdbcli

You should see the following after installing the client:

Installing collected packages: hdbcli
Successfully installed hdbcli-2.x.xxx
Log on to answer question
Step 2: Establish a test connection

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

  • Database host
  • Database port
  • Database user
  • Password
#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:

python3 connect.py

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

connected
Log on to answer question
Step 3: Hide sensitive connection parameters

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.

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.

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)

It isn’t recommended to deploy a production application of this type on Windows.

Export your environment variables

  • Database host
  • Database port
  • Database user
  • Password
Windows Environment Variables

We’ll use the os package from Python to access these user environment variables.

from hdbcli import dbapi
import os

conn = dbapi.connect(
    address=os.getenv('HOST'),
    port=os.getenv('PORT'),
    user=os.getenv('DB_USER'),
    password=os.getenv('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:

python connect.py
Enter console output below to validate your connection.
×

Next Steps

Prerequisites

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