如何通过ibm_db python包

时间:2018-02-23 18:55:10

标签: python db2 odbc dsn

我正在尝试通过python连接到本地DB2数据库,但过去几天一直没有成功。我希望社区可以帮助我破译错误信息并提供他们的经验帮助。

我的目标是在我的Windows 7笔记本电脑上安装Python ibm_db库并连接到我们的DB2实例

我无法让ibm_db.connect()方法识别已知良好的系统DSN或用户DSN(我使用它们通过Sql Server Management Studio(SSMS)连接到DB2)

我从各种连接组合中得到以下错误(如下所示),但我没有找到任何参考,说明错误的含义,以及我可以做些什么来解决问题。我搜索并阅读了几十个IBM网站和文档,但没有提到这些信息。   - >我有一种感觉,我错过了一些简单的步骤,但我不知道我现在还不知道:)

系统配置注意事项::

  • Windows 7笔记本电脑

  • DB2版本7.3

  • Python 2.7.13 | Anaconda 4.4.0(64位)| (默认,2017年5月11日,13:17:26)[win v300 64位(AMD64)] win32

  • 安装了似乎相关的Python库版本

  • ibm-db(2.0.8)
  • ibm-db-sa(0.3.3)
  • pyodbc(4.0.16)
  • pyOpenSSL(17.0.0)

  • 我安装并运行了iSeries Navigator,其中包括iSeries ODBC驱动程序。它完美地连接到本地DB2实例

  • 我安装了DB2命令行界面,它在注册表和64位ODBC管理器驱动程序列表中添加了名为IBM DB2 ODBC DRIVER - C_clidriver的DRIVER

  • 这是我使用的iSeries ODBC驱动程序创建的文件DSN,但与我用于IBM CLI驱动程序的DSN完全不同:

    [ODBC] DRIVER = iSeries Access ODBC驱动程序 DATABASE = DBASE PKG = QGPL / DEFAULT(IBM),2,0,1,0,512 LANGUAGEID = ENU DFTPKGLIB = QGPL DBQ = QGPL SYSTEM = DBSYS

首次尝试::指定完整DSN的基本连接:

>>> import ibm_db
>>> dsn = "DATABASE=dbname;HOSTNAME=hostname.domain;PORT=466;PROTOCOL=TCPIP"
>>> conn = ibm_db.connect(dsn,'mytextuid','password')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
Exception: [IBM][CLI Driver] SQL10007N Message "0" could not be retrieved.  
  **Reason code: "6". SQLCODE=-1366**

下一步::在注册表中添加名称为EXACT驱动程序名称的驱动程序

>>> dsn = "DRIVER={IBM DB2 ODBC DRIVER - C_clidriver};HOSTNAME=hostname.domain;PORT=446;PROTOCOL=TCPIP;UID=mytextuid;PWD=password;"
>>> conn = ibm_db.connect(dsn,'CROWLEY','July2017')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
Exception: [IBM][CLI Driver]  
  **SQLSTATE=01S00 SQLCODE=-99999**

下一个::尝试使用不同格式的DSN,结合使用DATABASE,HOSTNAME和PORT

>>> dsn = "DATABASE=Dbname:hostname.domain:466;PROTOCOL=TCPIP"
>>> conn = ibm_db.connect(dsn,'mytextuid','password')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
Exception: [IBM][CLI Driver] SQL10007N Message "0" could not be retrieved.  
  **Reason code: "6". SQLCODE=-1024**

下一步::尝试一个已知的系统DSN,因为我在SSMS中使用它

>>> conn = ibm_db.connect('DSN=SYSTEM_DB2_DSN','mytextuid','password')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
Exception: [IBM][CLI Driver] SQL10007N Message "0" could not be retrieved.  
  **Reason code: "6". SQLCODE=-1013**

下一步::尝试使用与上述配置相同的用户DSN

>>> conn = ibm_db.connect("DSN=USER_DB2_DSN","mytextuid","password")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
Exception: [IBM][CLI Driver]  
  **SQLSTATE=HY009 SQLCODE=-99999**

下一步::在ODBC系统DSN上启用跟踪(结果:ODBC管理员在连接时挂起)

C:\clidriver\db2trace.log>cat p8516t4948.cli


[ Process: 8516, Thread: 4948 ]
[ Date & Time:               02/27/2018 15:28:34.282526 ]
[ Product:                   QDB2/NT64 DB2 v11.1.2021.1452 ]
[ Level Identifier:          0203010F ]
[ CLI Driver Version:        10.01.0000 ]
[ Informational Tokens:      "DB2 v11.1.2021.1452","s1709221349","DYN1709221349W
IN64","Fixpack 2a" ]
[ Install Path:              C:\clidriver ]
[ db2cli.ini Location:       C:\Users\mytextuid\db2cli.ini ]
[ db2cli.ini Location2:      C:\ProgramData\IBM\DB2\C_clidriver\cfg\db2cli.ini ]

[ db2dsdriver.cfg Location:  C:\ProgramData\IBM\DB2\C_clidriver\cfg\db2dsdriver.
cfg ]
[ CLI Driver Type:           IBM Data Server Driver For ODBC and CLI ]



[0000008516 0000004948] [02/27/2018 15:28:34.283197] SQLAllocEnv(phEnv=&0000000000392b00 )
[0000008516 0000004948] [02/27/2018 15:28:34.284702]     ---> Time elapsed - 0 seconds

[0000008516 0000004948] [02/27/2018 15:28:34.299819] SQLAllocEnv( phEnv=0:1 )
[0000008516 0000004948] [02/27/2018 15:28:34.301458]     <--- SQL_SUCCESS   Time elapsed - +1.826100E-002 seconds

[0000008516 0000004948] [02/27/2018 15:28:34.302199] SQLAllocConnect( hEnv=0:1,phDbc=&0000000000392b08 )
[0000008516 0000004948] [02/27/2018 15:28:34.304124]     ---> Time elapsed - +7.410000E-004 seconds

[0000008516 0000004948] [02/27/2018 15:28:34.305055] SQLAllocConnect( phDbc=0:1)
[0000008516 0000004948] [02/27/2018 15:28:34.305902]     <--- SQL_SUCCESS   Time
 elapsed - +3.703000E-003 seconds

[0000008516 0000004948] [02/27/2018 15:28:34.306285] SQLDriverConnect( hDbc=0:1,hwnd=0:0,szConnStrIn="DSN=DSNNAME;UID=mytextuid;PWD=********;AUTOCOMMIT=0;CONNECTTYPE=1;", cbConnStrIn=-3, szConnStrOut=&0000000000103230, cbConnStrOutMax=256,pcbConnStrOut=&00000000001031f6,fDriverCompletion=SQL_DRIVER_NOPROMPT )
[0000008516 0000004948] [02/27/2018 15:28:34.308920]     ---> Time elapsed - +3.830000E-004 seconds
    sqlccconnr( timeout - +0.000000E+000, protocol - 0x03 )

下一个::观看c:\ ProgramData \ IBM \ DB2 \ C_clidriver \ db2diag.log

我在ODBC Admin中测试了DSN的连接,这是我收到的条目...注意::“DB”,APPID和HOSTNAME在下面被混淆了...我不知道为什么使用此驱动程序需要许可证,如果这是问题

2018-02-27-16.29.43.560000-480 E27622F847           LEVEL: Error
PID     : 13452                TID : 4948           PROC : odbcad32.exe
INSTANCE:                      NODE : 000           DB   : DBNAME
APPID   : my.ipa.ddr.ess.65253.180228002943
HOSTNAME: ${COMPUTERNAME}
EDUID   : 4948
FUNCTION: DB2 UDB, Connection Manager, sqleuPerformServerActivationCheck, probe:110
MESSAGE : ADM12008C  The product "IBM Data Server Driver For ODBC and CLI" does 
          not have a valid license key installed and the evaluation period has 
          expired. Functions specific to this product are not enabled. If you 
          have licensed this product, ensure the license key is properly 
          installed. You can install the license using the db2licm command. The 
          license file can be obtained from your licensed product CD.

1 个答案:

答案 0 :(得分:0)

试试这个(Python3和Python2):

import ibm_db as db2

def make_connexion(server, port, database, username, password, timeout):
    return db2.connect('DATABASE={0};'.format(database) +
                  'HOSTNAME={0};'.format(server) +
                  'PORT={0};'.format(str(port)) +
                  'PROTOCOL=TCPIP;' +
                  'UID={0};'.format(username) +
                  'PWD={0};'.format(password) +
                  'ConnectTimeout={0};'.format(str(timeout))
                  , '', '')

# sample connexion object
connexion = make_connexion('localhost', '50000', 'database', 'username', 'password', '30')