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.
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.
Note:Even if you are not aware of python, no worries. We will cover each and everything in detail for you.
Python API:
There are several APIs available to connect python to SAP HANA. In this article we will use one of the simplest one which is DB API .
dbapi API is Python DB API 2.0.
Configure Python API in SAP HANA:
1. 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
2. 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
3. Copy pyhdbcli.pdb, pyhdbcli.pyd files from hdbclient folder.
By default this location will be "C:/Program Files/sap/hdbclient"
4. 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:
1. Write below code in a notepad and save with .py extension.
import dbapi
## 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
print conn.isconnected()
# create a table
cursor = conn.cursor()
stmnt = 'Create column table SCHEMA1.table1 (ID integer, name varchar(10))'
cursor.execute(stmnt)
print 'table created'
# insert some data into table
stmnt = 'insert into SCHEMA1.table1 values (?, ?)'
cursor.execute(stmnt, (1,'A'))
cursor.execute(stmnt, (2,'B'))
print '2 records inserted into table'
# fetch table data
stmnt = 'select * from SCHEMA1.table1'
cursor.execute(stmnt)
result = cursor.fetchall()
print result
- 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
No comments:
Post a Comment