pyodbc.Error 'IM002' connecting to DB2

醉酒当歌 提交于 2020-01-05 06:29:07

问题


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.


回答1:


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.




回答2:


It does work using pyodbc. I think you have a wrong connection string. After some research and tests I solved with this code:

con = pyodbc.connect('DRIVER=iSeries Access ODBC Driver;SYSTEM=10.0.0.1;UID=bubi;PWD=xyz;DBQ=DEFAULTSCHEMA;EXTCOLINFO=1')
cur = con.cursor()
cur.execute('select * from MYTABLE')
row = cur.fetchone()
if row:
    field1 = row[0] 
    field2 = row[1]
# etc...

As you see it doesn't need a DSN to be configured on your system.




回答3:


This connection string for pyodbc, work for me:

conexion_str = 'SYSTEM=%s;db2:DSN=%s;UID=%s;PWD=%s;DRIVER=%s;' % (self._SYSTEM, self._DSN, self._UID, self._PWD, self._DRIVER)
self._cnn = pyodbc.connect(conexion_str)


来源:https://stackoverflow.com/questions/4452322/pyodbc-error-im002-connecting-to-db2

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!