pyodbc从DB2的存储过程返回多个游标 [英] pyodbc return multiple cursors from stored procedure with DB2

查看:405
本文介绍了pyodbc从DB2的存储过程返回多个游标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个python程序从db2数据库调用存储过程。我使用 results = cursor.fetchall()来处理我的存储过程的结果。但是,我的存储过程返回两个游标。 结果仅包含第一个。我需要一种方法来循环遍历尽可能多的光标。我希望 fetchmany()将是我的答案,但不是。



我真的需要能够做多个结果集,因为我正在编写的程序只能调用一个存储过程。要回去使用它能够调用两个需要很多。除了这些事情之一,我需要使10个游标返回。一切都是动态的,所以应用程序不知道它正在运行什么程序,它只是获取数据并将其吐到excel不知道的意义。我需要一个游标用于数据,其他游标用于不同类型的计数和总计。



请帮助。让我知道,如果你需要更多的信息,我不认为精心编写的代码是必要的,因为我正在寻找一个内置的功能来做到这一点,甚至可能是一个不同的图书馆,因为我已经完成了我的份额的谷歌搜索,它看起来像pyodbc不会为DB2执行此操作。 DB2是一个要求:(

解决方案

使用游标的nextset()方法: http://code.google.com/p/pyodbc/wiki/Cursor#nextset



示例代码:

 #从第一集提取行
rows = cursor .fetchall()
#process first set rows here

#提前到下一个结果集
while(cursor.nextset()):
#fetch rows from next设置,首先丢弃
rows = cursor.fetchall()
#process next set rows here

nextset()将返回True,如果其他结果集可用,随后的游标提取方法将从下一个集合返回行。如果没有附加集合可用,该方法返回None


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 REALLY 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.

Please Help. Let me know if you need any more information, I didn't think elaborate code was necessary as 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 :(

解决方案

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.

这篇关于pyodbc从DB2的存储过程返回多个游标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆