This article explains how to connect to a SAP HANA system using the Python hdbcli package. The package is available as part of the SAP HANA HDB client software. The client software can be downloaded when installing HANA Express.
Once the client software is installed, locate the file hdbcli-x-y-z.tar.gz. The next step is to install the Python hdbcli package. This can be done easily using the Python utility program pip. Execute the following command in a terminal.
pip install /path/to/hdbcli-x-y-z.tar.gz After the process completes, the Python hdbcli package will be installed. Listing 1 below shows a simple script that tests the database connection.
Replace the connect arguments to reflect your own systems credentials. The SERVER_HOST can be an IP address or a host name.
The script will print True when a successful connection is made.
SQL statements are executed on a Cursor object. Listing 2 below shows a simple SQL statement that returns the current date.
2cursor = connection.cursor()
4cursor.execute('SELECT NOW() FROM DUMMY')
6row = cursor.fetchone()
Notice on line 2, the cursor() method is used to return a Cursor object. This object is then used to execute SQL statements.
The execute() method returns a Boolean value indicating if the statement executed successfully. The Cursor object exposes several methods to return data. The fetchone() method is used to return a single row while fetchall() returns multiple rows.