简体   繁体   English

在代码中使用没有 UID 和 PWD 的 HDODBC 驱动程序连接到 SAP HANA

[英]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.我正在尝试通过 Python 代码连接到 SAP HANA 数据源。 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.但是,我不希望字符串中包含 UID 和 PWD 字段。 I did set up a DSN connection using the ODBC manager on Windows.我确实使用 Windows 上的 ODBC 管理器设置了 DSN 连接。 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?如何在 python 代码中不显示我的 UID 和 PWD 的情况下设置连接?

I have been looking for the same option to use hdbuserstore key to be used with python for connecting to SAP HANA. 我一直在寻找将hdbuserstore密钥与python一起使用以连接到SAP HANA的选项。 Looks like HDB client hdbcli has that option added now. 看起来HDB客户端hdbcli现在已添加了该选项。

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. 运行脚本的用户需要有PYTHON_PATH设置到的位置hdbclient或者你可以有路径设置脚本。

from hdbcli import dbapi

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

conn.isconnected() will return True if the connection is successful. 如果连接成功, conn.isconnected()将返回True。

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 . SAP HANA客户端软件(该软件包还包含ODBC驱动程序)提供了一个程序来为登录数据设置安全存储: 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. 为应使用该应用程序的操作系统用户创建hdbuserstore条目。

     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. 必须在ODBC连接中引用hdbuserstore密钥。 To do that, fill the SERVERNODE parameter with @<KEYNAME> instead of the actual server address. 为此,请使用@<KEYNAME>而不是实际服务器地址填​​充SERVERNODE参数。
    For the example above, the value would be @millerj . 对于上面的示例,该值为@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. ODBC驱动程序将尝试查找连接时提供的hdbuserstore条目,并使用该条目连接到数据库。

Check the documentation for more information on this. 有关更多信息,请查阅文档

Be carefull with parameter CONNECTTIMEOUT=5.小心参数 CONNECTTIMEOUT=5。

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

This means NOT 5 second because it is in ms.这意味着不是 5 秒,因为它以毫秒为单位。 Toke me long time to find out this problem.花了我很长时间才发现这个问题。

  • connectTimeout, Timeout in milliseconds connectTimeout,以毫秒为单位的超时时间
  • 0 (use system's TCP/IP socket connection timeout) 0(使用系统的 TCP/IP 套接字连接超时)
  • Aborts connection attempts after the specified timeout.在指定的超时后中止连接尝试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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