首页 文章

如何通过ibm_db python包连接到DB2

提问于
浏览
0

我试图通过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)[MSC v.1500 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 Command Line接口,它在注册表和64位ODBC Manager驱动程序列表中添加了名为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

First Attempt :: basic connection with a full DSN specified:

>>> 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**

Next :: Add a driver with name with EXACT driver name as is in the registry

>>> 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**

Next :: try a different format DSN, combining DATABASE, HOSTNAME, AND 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**

Next :: try a System DSN that is Known Good because I use it in 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**

Next :: try a User DSN with same configuration as above

>>> 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**

Next :: Enabled Trace on the ODBC System DSN (result: ODBC Admin Hangs on connection)

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 )

Next :: watching 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

    试试这个(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')
    

相关问题