我正在试验Teradata python模块教程here
我正在执行一个查询,我想多次迭代结果集。问题是如果我迭代结果集一次,我不能再做了。看起来结果集已用完,无法再进一步计算。
有关详细信息,请参阅以下代码,并建议如何保留结果集。
import teradata
class DB():
def __init__(self):
udaExec = teradata.UdaExec (appName="HelloWorld", version="1.0",logConsole=False)
session = udaExec.connect(method="odbc", system="tddemo",username="dbc", password="dbc")
self.session = session
def fun1(self):
rows = self.session.execute("SELECT databasename, ownername FROM DBC.DATABASES where DatabaseName='financial'")
return rows
db = DB()
rows = db.fun1()
# This loop prints accurate result like
#Row 1: [financial, Samples]
for row in rows:
print(row)
# This loop does not print anything
for row in rows:
print(row)
# This line also gets printed
print("The End")
答案 0 :(得分:2)
生成器只能迭代一次,使用list
或tuple
进行转换,然后您可以多次迭代它:
rows = tuple(db.func1())
for row in rows:
# do something
for row in rows: # would work
# do something else