简体   繁体   English

通过 pyodbc 从 Python 连接到 Db2 时出错

[英]Error connecting from Python to Db2 through pyodbc

I installed the Db2 ODBC driver in Windows 10 following these instructions .我按照这些说明在 Windows 10 中安装了 Db2 ODBC 驱动程序。 I extracted the driver folder in C:\tools\db2cli\ and run db2cli install -setup from the bin folder.我在C:\tools\db2cli\中提取了驱动程序文件夹,然后从bin文件夹运行db2cli install -setup

This seems to have worked fine, as the ODBC driver was registered in Windows:这似乎工作正常,因为 ODBC 驱动程序已在 Windows 中注册:

在此处输入图像描述

But when I try to connect from Jupyter I get this error:但是当我尝试从Jupyter连接时,我收到了这个错误:

server = 'localhost' 
database = 'mydb' 
username = 'db2inst1' 
password = 'abc123' 
driver = 'IBM DB2 ODBC DRIVER - C_tools_db2cli'
conn = pyodbc.connect('DRIVER={' + driver +'};SERVER='
         + server + ';DATABASE=' + database + ';UID=' 
         + username + ';PWD=' + password + ';PROTOCOL=TCPIP;PORT=50000' )

Error Traceback (most recent call last) in 4 password = 'abc123' 5 driver = 'IBM DB2 ODBC DRIVER - C_tools_db2cli' ----> 6 conn = pyodbc.connect('DRIVER={' + driver +'};SERVER=' 7 + server + ';DATABASE=' + database + ';UID=' 8 + username + ';PWD=' + password + ';PROTOCOL=TCPIP;PORT=50000' ) 4 密码 = 'abc123' 5 驱动程序 = 'IBM DB2 ODBC 驱动程序 - C_tools_db2cli' ----> 6 conn = pyodbc.connect('DRIVER='}; =' 7 + 服务器 + ';DATABASE=' + 数据库 + ';UID=' 8 + 用户名 + ';PWD=' + 密码 + ';PROTOCOL=TCPIP;PORT=50000' )

Error: ('HY000', '[HY000] [IBM][CLI Driver] SQL1042C An unexpected system error occurred. SQLSTATE=58004\r\n (-1042) (SQLDriverConnect); [HY000] [IBM][CLI Driver] SQL1042C An unexpected system error occurred. SQLSTATE=58004\r\n (-1042)')错误: ('HY000', '[HY000] [IBM][CLI Driver] SQL1042C 发生意外的系统错误。SQLSTATE=58004\r\n (-1042) (SQLDriverConnect); [HY000] [IBM][CLI Driver] SQL1042C 发生意外的系统错误。SQLSTATE=58004\r\n (-1042)')

I unsuccessfully tried to find the error codes to find a solution.我试图找到错误代码以找到解决方案,但没有成功。 What should be looked at to fix this problem?应该看什么来解决这个问题?

Note: I added C:\tools\db2cli\bin to the PATH but still get the same error.注意:我将C:\tools\db2cli\bin添加到 PATH 但仍然得到相同的错误。

This issue was caused by a defect in clidriver for some specific configurations on Microsoft Windows.此问题是由 Microsoft Windows 上某些特定配置的 clidriver 缺陷引起的。

You can workaround this by ensuring that the PATH does not have any other Db2-products on it (except for clidriver\bin ), and then ensuring to add two additional directories to the PATH before launching python.您可以通过确保 PATH 上没有任何其他 Db2 产品( clidriver\bin除外),然后确保在启动 python 之前将两个附加目录添加到 PATH 来解决此问题。 These are the fully qualified pathnames to these directories:这些是这些目录的完全限定路径名:

clidriver\bin\icc64
clidriver\bin\amd64.vc12.crt

IBM should at some point deliver a fix for IT34327. IBM 应该在某个时候为 IT34327 提供修复程序。

SERVER= is for the already cataloged database SERVER= 用于已编目的数据库

otherwise you must use: HOSTNAME= if you want to specify full DSN.否则你必须使用:HOSTNAME= 如果你想指定完整的 DSN。 https://cloud.ibm.com/docs/Db2onCloud?topic=Db2onCloud-con_program&locale=fr https://cloud.ibm.com/docs/Db2onCloud?topic=Db2onCloud-con_program&locale=fr

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

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