簡體   English   中英

如何遍歷python中來自sql中存儲過程的表。 使用 While 循環和 Pyodbc?

[英]How to loop through tables in python that come from a stored procedure in sql. With While Loop and Pyodbc?

cur = connection.cursor()
cur.execute("set nocount on exec SP") 
while(cur.nextset()):
    print(cur.description)

此代碼生成 10 個表,有時是 11 個。我希望能夠使用 while 循環,因為每次表的數量可能不同。 上面這段代碼的問題是它生成 9 個表而不是 10 個,因為 cur.nextset() 運行並跳過第一個表。

我能做些什么來解決這個問題? 我希望代碼在循環中工作。 我需要一個與 pyodbc 模塊一起使用的更好的函數。

有任何想法嗎?

您可以通過將nextset()檢查移動到循環底部來避免跳過第一個結果集:

cur.execute("set nocount on exec SP")
while True:
    # do stuff with the result set
    if not cur.nextset():
        break
print("All result sets have been processed.")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM