pyodbc 使用 DB2 从存储过程返回多个游标

发布于 2024-12-02 15:34:46 字数 454 浏览 1 评论 0原文

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

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

我正在寻找一个内置函数来执行此操作,或者甚至可能是一个不同的库,因为我已经完成了谷歌搜索,看起来 pyodbc 并没有为 DB2 执行此操作。 DB2 是一个要求。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

舟遥客 2024-12-09 15:34:46

使用光标的 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,并且后续光标fetch 方法将从下一组返回行。如果没有其他可用集,该方法将返回 None

Use the nextset() method of the cursor: https://github.com/mkleehammer/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.

世界等同你 2024-12-09 15:34:46

只是为了记录而做一个小小的简化:

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

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    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文