pyodbc return multiple cursors from stored procedure with DB2

穿精又带淫゛_ 提交于 2019-12-11 07:43:41

问题


I have a python program that calls a stored procedure from db2 database. I am using results = cursor.fetchall() to process the results of my stored procedure. However, my stored procedure returns two cursors. results only contains the first one. I need a way to loop through as many cursors as I want. I was hoping fetchmany() would be my answer but it is not.

I need to be able to do multiple result sets as the program I am writing can only call one stored procedure. It would take a lot to go back and make it to be able to call two. Besides with one of these things I need to make 10 cursors return. Everything is dynamic, so the application doesn't know what procedure it is running, it just gets the data and spits it into excel not knowing the meaning. I need one cursor for the data, and the other cursors for different types of counts and totals.

I am looking for a built in function to do this, or maybe even a different library because I have done my share of googling and it looks like pyodbc does not do this for DB2. DB2 is a requirement.


回答1:


Use the nextset() method of the cursor: http://code.google.com/p/pyodbc/wiki/Cursor#nextset

Sample code:

# fetch rows from first set
rows = cursor.fetchall()    
# process first set rows here

# advance to next result set
while (cursor.nextset()):    
    # fetch rows from next set, discarding first
    rows = cursor.fetchall()    
    # process next set rows here

nextset() will return True if additional result sets are available, and subsequent cursor fetch methods will return rows from the next set. The method returns None if no additional sets are available.




回答2:


Just a small simplification for the record:

while True:    
    rows = cursor.fetchall()
    # process all result sets in the same place
    if not cursor.nextset():
        break    


来源:https://stackoverflow.com/questions/7263781/pyodbc-return-multiple-cursors-from-stored-procedure-with-db2

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