We have a Chinese version of this blog
Python is a simple scripting language, it is good at text processing and network programming,especially in Linux, because for various release version of Linux they contain Python. For some simple data processing task , if we use python to access SAP HANA it will be very simple. And inside SAP HANA, many test task is also done by python.
In SCN ,there is one blog ”SAP HANA and Python? Yes Sir! “(http://scn.sap.com/community/developer-center/hana/blog/2012/06/08/sap-hana-and-python-yes-sir) which introduced the usage of python for SAP HANA under windows. In Linux platform, it is a little different, this article will show you how to use python in Linux platform.
1 environment set up
The first thing is to download the SAP HANA client for Linux(SAP_HANA_CLIENT), after the download , decompress the package then you will get a file list like this:
just execute the command "./hdbinst" and follow the instructions which lead you to complete the installation.
The default installation path is "/usr/sap/hdbclient"
After the installation, the file list is as follows:
You will see there is a directory names "Python" . This is the Python which shipped within SAP HANA CLIENT. Before you can use it you need to do some work. copy "dbapi.py","__init__.py","resultrow.py" which under the directory hdbcli to the destination: /usr/sap/hdbclient/Python/lib/python2.6. At the same time, copy the file "pyhdbcli.so" to the same directory.
Then you can execute "/usr/sap/hdbclient/Python/bin/python"
generally as long as the "import dbapi" does not complains about error, the environment is OK. But it seems not cool to use the python package which shipped inside SAP HANA Client. You may wonder if you can use your system's python to connect to SAP HANA, integrate them together, using the Linux's python to access HANA, this will be cool.
In fact, you may discovered that for python to access SAP HANA,the key is the 4 files, the problem is , where should you put these 4 files for the Linux's python to access SAP HANA. If you are clean about this, for a new machine to access SAP HANA, you need not to install the client again ,just copy these 4 files. So we need to find the library for Python of 'Linux'.
You can execute "whereis python" to locate python (for now my test environment is SUSE Linux 11 x86_64 sp2)
python: /usr/bin/python2.6 /usr/bin/python /usr/lib64/python2.6 /usr/lib64/python /usr/bin/X11/python2.6 /usr/bin/X11/python /usr/local/bin/python /usr/local/bin/python2.7-config /usr/local/bin/python2.7 /usr/local/lib/python2.7 /usr/include/python2.6 /usr/share/man/man1/python.1.gz
You may need to try a few times to decide which is the correct path.
In my machine, I found that copy the file "dbapi.py","__init__.py","resultrow.py", "pyhdbcli.so" to the directory "/usr/local/lib/python2.7" , then the Linux system's python can access to SAP HANA.
But I think this is depends on the Linux distribution's version. So it is may be different in you machine.
2 simple demo
#import the database connect api import dbapi #argument serverAdress='<your IP>' serverPort=<port> userName='<username>' passWord='<password>' #connect to hana database conn=dbapi.connect(serverAdress,serverPort,userName,passWord) #query query="select idno,name FROM WEIYY_TEST.PTAB1" cursor=conn.cursor() try: ret=cursor.execute(query) ret=cursor.fetchall() for row in ret: for col in row: print col, print except Exception,ex: print ex #insert data query="insert into WEIYY_TEST.PTAB1(IDNO,NAME) values('111','hello,world')" try: ret=cursor.execute(query) except Exception,ex: print ex #close connection and cursor conn.close() cursor.close()
The code above will login to a database and execute a query command and then insert a new record:
then we execute it again:
it shows the record which I have inserted, and in the exception process part there is a 'unique constraint violated" error.
Now you can see that it is really easy to use python to connect to SAP HANA, a few lines of code will finish the task.
[SAP HANA version of the test cases used here is SAP HANA SPS7 Revision 70.00]