Trouble connecting to Db2 for IBM i using pyodbc

Viewed 1453

im trying to make a connection to an as400 with db2 using pyodbc and the ibm db2 odbc driver.

import pyodbc

connection = pyodbc.connect(
    driver='{IBM DB2 ODBC DRIVER}',
    system='192.168.1.100',
    uid='user',
    pwd='pass')
c1 = connection.cursor()

#this is meaningless sql, i just want the connection
c1.execute('select * from libname.filename')
for row in c1:
    print (row)

Running this gives me this error

python pydata.py

Traceback (most recent call last):
  File "C:\Users\tca\Desktop\ScriptingSTuff\pydata.py", line 3, in <module>
    connection = pyodbc.connect(
pyodbc.OperationalError: ('08001', '[08001] [IBM][CLI Driver] SQL1013N  The database alias name or database name "" could not be found.  SQLSTATE=42705\r\n (-1013) (SQLDriverConnect)')

Any ideas?

This is all under win10

EDIT: Adding this "database='s10c38ft',"

import pyodbc

connection = pyodbc.connect(
    driver='{IBM DB2 ODBC DRIVER}',
    system='192.168.1.100,8471',
    database='s10c38ft',
    uid='user',
    pwd='pass')
c1 = connection.cursor()

c1.execute('select * from libname.filename')
for row in c1:
    print (row)

Makes it hang on a blinking cursor, I cant even CTRL+C to end it, I have to close cmd.

1 Answers

The proper driver name should be IBM i Access ODBC Driver (but see notes below). Other than that, your first example was correct:

connection = pyodbc.connect(
    driver='{IBM i Access ODBC Driver}',
    system='192.168.1.100',
    uid='user',
    pwd='pass')

If that doesn't work, there are two main possibilities:

  1. You are using an old ODBC driver. This would happen if you are using the old iSeries Access (in which case the driver name is iSeries Access ODBC Driver) or even older Client Access (driver name Client Access ODBC Driver (32-bit)). If you choose the appropriate name for your driver, it will work.

  2. You are using an ODBC driver that is not for IBM i. The most commonly used member of the Db2 family is Db2 for LUW (Linux, Unix, Windows), but there are others. None of these will work for you.

You can find out the list of exact ODBC driver names you have installed by calling pyodbc.drivers(). If you don't have any of the ones I mentioned above by name, then you don't have the right driver. The ODBC driver you want is the one described here.

Related