mojo_28
mojo_28

Reputation: 73

Connect to HANA from Python

I am trying to connect to HANA in order to pull some metadata in a pandas dataframe. There are lots of mixed approaches and I couldn't find anything concrete.

All I have is:

The admin has provided all the read access to the required account for the specific tables.

What is the quickest way to get this done? I do not have the option of installing anything on SAPs site.

I have tried the below snippets but I get the error 'target machine actively refused it' and to debug at SAPs end is a lost cause. Thank you in advance.

import pyhdb
connection = pyhdb.connect(
     host="123.com",
     port=123,
     user="user",
     password="pswrd"
 )
 cursor = connection.cursor()
 cursor.execute("SELECT * FROM Tablename")
 cursor.fetchone()
 connection.close()

and

from hdbcli import dbapi
conn = dbapi.connect(
        address="123.com",
        port=123,
        user="user",
        password="pswrd"
    )
    cursor = conn.cursor()

Upvotes: 3

Views: 11416

Answers (2)

Ajay Pandey
Ajay Pandey

Reputation: 277

To connect to hana DB :

from hdbcli import dbapi
conn = dbapi.connect(
   address="serverhost",
   port=39015,
   user="UserName",
   password="Password",
   databasename='DBNAME'
)

Make sure you enter the correct port number

To get the sql results in pandas dataframe:

query = 'select * from table'
df = pd.read_sql_query(query, conn)
df.head()

Upvotes: 3

Lars Br.
Lars Br.

Reputation: 10396

Given your server address and port examples, I'm not sure you got the right idea for how to connect to a HANA database.

Since you want to use pandas it is probably a good idea to have a look at the SAP HANA Machine Learning library for Python.

Check the tutorial blog post for this: https://blogs.sap.com/2019/11/05/hands-on-tutorial-machine-learning-push-down-to-sap-hana-with-python/

To do any of this, there is no need to install or debug anything on the HANA system.

Upvotes: 2

Related Questions