pyodbc.错误“IM002”连接到 DB2
我下载了 Python 2.7 (python-2.7.1.amd64.msi) 和 pyodbc,用于连接 DB2 数据库的 python 扩展模块 (即 pyodbc-2.1.8.win-amd64-py2 .7.exe)。
我编写了示例脚本,如下所示。
import csv
import pyodbc
conn = pyodbc.connectpyodbc.connect('DRIVER={DB2};SERVER=localhost;DATABASE=DBT1;UID=scott;PWD=tiger;')
curs = conn.cursor()
curs.execute('select count(edokimp_id) from edokimp')
print curs.fetchall()
该脚本抛出以下错误
pyodbc.Error: ('IM002', '[IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified (0) (SQLDriverConnectW)')
由于我是 Python 新手,我从错误中意识到我需要下载 pyodbc 的 IBM DB2 驱动程序,因此在 Google 上进行了广泛搜索,但找不到任何内容。
如果您能指出我可以下载驱动程序的网站,并稍后向我解释如何配置/加载驱动程序,我将不胜感激。
如果是 Java,
- 驱动程序将以 ojdbc.jar 的形式提供,该文件将被复制到位于类路径上的 lib 目录中,
- 对配置文件进行更改,
- 引用来自 Java 类的数据源
我是 Python 的新手,所以如果您能让我知道 Python 中的示例的相应步骤,我将不胜感激。
I downloaded Python 2.7 (python-2.7.1.amd64.msi) and pyodbc, the python extension module for connecting to DB2 database (i.e. pyodbc-2.1.8.win-amd64-py2.7.exe).
I wrote sample script as shown below.
import csv
import pyodbc
conn = pyodbc.connectpyodbc.connect('DRIVER={DB2};SERVER=localhost;DATABASE=DBT1;UID=scott;PWD=tiger;')
curs = conn.cursor()
curs.execute('select count(edokimp_id) from edokimp')
print curs.fetchall()
The script throws following error
pyodbc.Error: ('IM002', '[IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified (0) (SQLDriverConnectW)')
As I am a newbie to Python, I realized from the error that I need to download the IBM DB2 driver for pyodbc and hence searched extensively on Google but couldn't find any.
I would greatly appreciate if you could point me to the site where I can download the driver and later explain me how to configure/load the driver.
In case of Java
- the driver will be shipped in the form of ojdbc.jar which will be copied to the lib directory which will be on classpath
- make changes to configuration file
- reference the DataSource from Java Class
I am newbie to Python so I would greatly appreciate if you could let me know cooresponding steps with an example in Python.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以在项目主页上获取PyDB2驱动。
如果您遇到官方 Python 的编译问题,ActivePython 是 Windows 上 Python 的一个很好的替代发行版。
编辑:如果它要求您提供 DB2 标头,您需要获取 用于 ODBC 和 CLI 的 IBM 数据服务器客户端。
You can get the PyDB2 driver on the project homepage.
If you run into compilation issues with the official Python, ActivePython is a good alternate distribution of Python on Windows.
Edit: If it asks you for DB2 headers, you need to get the IBM Data Server Client for ODBC and CLI.
使用 pyodbc 确实可以工作。我认为您的连接字符串错误。经过一些研究和测试,我用这段代码解决了这个问题:
如您所见,不需要在您的系统上配置 DSN。
It does work using pyodbc. I think you have a wrong connection string. After some research and tests I solved with this code:
As you see it doesn't need a DSN to be configured on your system.
pyodbc 的这个连接字符串对我有用:
This connection string for pyodbc, work for me: