Skip to content
Advertisement

Fetching Results after ibm_db.execute()

Take a look at the following code:

import ibm_db

#Authentication Details
f=open(r"C:UsersXXXXXMy Code.rc","r")
lines=f.readlines()
us=lines[0].rstrip()
pd=lines[1]

sql_file = "NNM_SQL.sql"
f = open(r"Z:MSGBACI-FAKCProjects\" + sql_file,"r")
sql = f.read()
conn_str = "DATABASE=DBMIDAS;HOSTNAME=abc.cx.dx.com;PORT=45000;PROTOCOL=TCPIP;UID="+us+";PWD="+pd+";"
conn = ibm_db.connect(conn_str,"","")
#result=ibm_db.exec_immediate(conn,sql)
stmt = ibm_db.prepare(conn,sql)
result = ibm_db.execute(stmt)

result_dict = ibm_db.fetch_assoc(result)
while result_dict is not False:
    print(result_dict)
    result_dict = ibm_db.fetch_assoc(result)
f.close()

This code sample returns the following error at the fetch_assoc() step.

Supplied statement object parameter is invalid

I dug around a bit more and found that when I use execute() statement, result is that of type Boolean.

But when I use exec_immediate(), result is that of type DBStatement.

Am I doing something wrong? The problem is, there is no sample code in IBM Knowledge Centre which fetches results after an “execute()”

Advertisement

Answer

You already pass in an object of type IBM_DBStatement to execute(). See the API reference for ibm_db.execute() for details. Thus, you need to pass in stmt instead of result. Try something like this:

conn = ibm_db.connect(conn_str,"","")
stmt = ibm_db.prepare(conn,sql)
result = ibm_db.execute(stmt)

result_dict = ibm_db.fetch_assoc(stmt)
while result_dict is not False:
    print(result_dict)
    result_dict = ibm_db.fetch_assoc(stmt)
f.close()
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement