Power of Python Integrated with SAP HANA
Python is a high level, object-oriented programming language for the web.
Python is Easy to Write, Easy to Read and Easy to Understand.
SAP HANA works pretty well with python too.
This article will give a basic idea of how to use python on top of SAP HANA.
Even if you are not aware of python, no worries. We will cover each and everything in detail for you.
There are several APIs available to connect python to SAP HANA. In this article we will use one of the simplest one which is
dbapi API is Python DB API 2.0.
Configure Python API in SAP HANA:
Navigate to the path where HANA client is installed and copy these 3 files.
__init__.py, dbapi.py, resultrow.py
By default it will saved in C:/Program Files/sap/hdbclient/hdbcli folder.
Note: If you have not installed the HANA client, then install it from here
Go to the python folder under HDBClient folder and copy all 3 files into the Lib folder.
By default the location will be C:/Program Files/sap/hdbclient/Python/Lib
Copy pyhdbcli.pdb, pyhdbcli.pyd files from hdbclient folder.
By default this location will be "C:/Program Files/sap/hdbclient"
Go to the same Python/Lib folder and paste these 2 files.
That's all.Configuration is done!!
Connect to SAP HANA and Run SQL Queries using Python:
Write below code in a notepad and save with .py extension.
## Replace SCHEMA1 with your schema
# assume HANA host id is abcd1234 and instance no is 00. user id is USER1 and password is Password1
conn = dbapi.connect('abcd1234', 30015, 'USER1', 'Password1')
#Check if database connection was successful or not
# create a table
cursor = conn.cursor()
stmnt = 'Create column table SCHEMA1.table1 (ID integer, name varchar(10))'
print 'table created'
# insert some data into table
stmnt = 'insert into SCHEMA1.table1 values (?, ?)'
print '2 records inserted into table'
# fetch table data
stmnt = 'select * from SCHEMA1.table1'
result = cursor.fetchall()
Provide your HANA system details and credential in the file.
Make sure you change the schema name with your schema.
Now save this file to hdbclientPython folder.
The default location of Python folder is "C:/Program Files/sap/hdbclient/Python"
Open Command prompt. Navigate to Python path and run the command
Support us by sharing this article.