hshantanu
hshantanu

Reputation: 454

Connect to SAP HANA by using HDODBC driver without UID and PWD in code

I am trying to connect to SAP HANA data source via Python code. I did manage to establish a connection. I have a raw data string in my code as follows:

db = pyodbc.connect(driver = '{HDBODBC}', UID='username', PWD='password', SERVERNODE='server:<port_no>')

However, I do not want the UID and PWD fields in my string. I did set up a DSN connection using the ODBC manager on Windows. But, I still need to enter my username and pwd as follows:

db = pyodbc.connect(DSN="MyDSN", UID='username', PWD='password')

How can I set up a connection without my UID and PWD being displayed in the python code?

Upvotes: 1

Views: 3075

Answers (4)

Dieter Perlach
Dieter Perlach

Reputation: 11

Be carefull with parameter CONNECTTIMEOUT=5.

from hdbcli import dbapi
conn = dbapi.connect(key='hdbuserstore key',CONNECTTIMEOUT=5)

This means NOT 5 second because it is in ms. Toke me long time to find out this problem.

  • connectTimeout, Timeout in milliseconds
  • 0 (use system's TCP/IP socket connection timeout)
  • Aborts connection attempts after the specified timeout.

Upvotes: 1

balaks80
balaks80

Reputation: 146

I have been looking for the same option to use hdbuserstore key to be used with python for connecting to SAP HANA. Looks like HDB client hdbcli has that option added now.

The user that is running the script needs to have the PYTHON_PATH set to the location of the hdbclient or in the script you can have the path set.

from hdbcli import dbapi

conn = dbapi.connect(key='hdbuserstore key',CONNECTTIMEOUT=5)

conn.isconnected() will return True if the connection is successful.

hope this is helpful for someone!

Upvotes: 2

Lars Br.
Lars Br.

Reputation: 10396

This requirement is relatively easy to fulfill. The SAP HANA client software (the package that also contains the ODBC driver) provides a program to set up a secure store for logon data: hdbuserstore.

In my blog I explained how that works in detail.

The core steps are

  1. create the hdbuserstore entries for the operating system user that should use the application.

    Syntax: hdbuserstore SET <KEY> <ENV> <USERNAME> <PASSWORD>
    Example: hdbuserstore SET millerj "localhost:30115" JohnMiller 2wsx$RFV
    
  2. The hdbuserstore key needs to be referred to in the ODBC connection. To do that, fill the SERVERNODE parameter with @<KEYNAME> instead of the actual server address.
    For the example above, the value would be @millerj.

And that's really all. The ODBC driver will try to look up the hdbuserstore entry provided upon connection and use that to connect to the database.

Check the documentation for more information on this.

Upvotes: 0

Nikolay Baranenko
Nikolay Baranenko

Reputation: 1675

for example create file in a secure place and load connection setting (UID, PWD encrypted password (heshkod)) from this file

Upvotes: 0

Related Questions