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

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

问题描述

我有一个从 db2 数据库调用存储过程的 python 程序.我正在使用 results = cursor.fetchall() 来处理我的存储过程的结果.但是,我的存储过程返回两个游标.results 只包含第一个.我需要一种方法来循环遍历任意数量的游标.我希望 fetchmany() 是我的答案,但事实并非如此.

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.

我需要能够处理多个结果集,因为我正在编写的程序只能调用一个存储过程.返回并使其能够调用两个需要很多.除了这些东西之一,我需要让 10 个游标返回.一切都是动态的,因此应用程序不知道它正在运行什么程序,它只是获取数据并将其吐入excel而不知道其含义.我需要一个游标用于数据,另一个游标用于不同类型的计数和总计.

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.

我正在寻找一个内置函数来做到这一点,或者甚至是一个不同的库,因为我已经完成了我的谷歌搜索,看起来 pyodbc 没有为 DB2 这样做.DB2 是一个需求.

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.

推荐答案

使用光标的nextset()方法:https://github.com/mkleehammer/pyodbc/wiki/Cursor#nextset

示例代码:

# 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() 将返回 True,并且后续的游标获取方法将返回下一组中的行.如果没有其他可用的集合,该方法返回 None.

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天全站免登陆