Aneesh Relan
Aneesh Relan

Reputation: 342

Cannot run db2 commands from pyodbc

I need to automate few IBM DB 2 commands through python via the pyodbc driver. I have successfully connected to the database by the DB2 ODBC Driver and can run SELECT, INSERT, CREATE TABLE commands. I need to run commands such as db2level, list node directory show detail, etc. which are showing exception as follows:

pyodbc.ProgrammingError: ('42601', '[42601] [IBM][CLI Driver][DB2/LINUXX8664] SQL0104N  An unexpected token "END-OF-STATEMENT" was found following "db2level".  Expected tokens may include:  "JOIN <joined_table>".  SQLSTATE=42601\n (-104) (SQLExecDirectW)')

This is my python pyodbc code:

import pyodbc

cnx = pyodbc.connect(
    'Driver={DB2}; '
    'Hostname=192.168.0.185; '
    'Port=50005; '
    'Protocol=TCPIP; '
    'Database=ABC; '
    'CurrentSchema=db2inst8; '
    'UID=db2inst8; '
    'PWD=12345;'
    )
cursor = cnx.cursor()
cursor.execute("db2level")

Upvotes: 1

Views: 796

Answers (1)

mao
mao

Reputation: 12267

the 'db2level' is an external program, it is not SQL, you cannot invoke it via pyodbc. You can invoke the command db2level in the same way you invoke any external command (as long as it is on the same hostname).

If you want to find the DB2 version/fixpack of the DB2 server using SQL then you can connect to the database and use db2-supplied views/functions for that purpose.

Upvotes: 1

Related Questions