简体   繁体   中英

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?

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!

例如,在安全的地方创建文件并从该文件加载连接设置(UID,PWD加密密码(heshkod))

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM