Connecting to SAP HANA through Python in windows 7
I am a part of Product Security APJ team which is developing a tool that uses MySQL as a database. Few days back we decided to move data from MySQL to SAP HANA due to performance issues. Due to this one of the major challenge that we faced was in making connection to SAP HANA through Python. It was hard to find discussions related to this problem which led to referring multiple HANA docs. Once we came up with a solution, we thought of consolidating the solution and make it available for others.
Here is a detailed explanation about how to connect to SAP HANA using Python in Windows 7.
Installing HANA client on Windows:
- Go to the folder where the DUMP (build of HANA client) is available using command prompt
2. Write the following command.
- hdbinst -a client
Screenshot of the installation is shown below :
3. Restart the windows system.
Connecting to the HANA DB through python:
To make DSN less connection:
- Once the HANA client is successfully installed , download and run the module : Pyodbc-3.0.6.win-amd64-py2.7.exe
2. To connect to the HANA DB through python use the below statement in your code:
- self.driver is name of the HANA driver installed (HDBODBC)
- self.server is path or location where the HANA DB resides. (Eg: inblrlxxx.dhcp.blrl.sap.corp:300XX)
- self.UID is the username for a particular database you want to connect.
- self.PWD is the password for a particular database you want to connect.
To make DSN connection:
- Open Data Source(ODBC) in windows, select System DSN and click on “Add button”.
2. It will ask for you to select a driver name from the list available. Select HDBODBC.
3. Then enter the details. Server:port should be the path or location where the HANA DB resides.
4. Click “Connect” to test the connection. Provide Username and password for the database you want to connect.
5. On successful connection, make use of the below code to make DSN connection through python:
- self.DSN is the name of the Data Source Name you provided in ODBC connection.
- self.UID is the username for the particular database you want to connect.
- self.PWD is the password for the particular database you want to connect.
Similarly to connect to SAP HANA through PHP in Windows 7, explanation is provide in my other post: Connecting to SAP HANA through PHP in windows 7