1

I am experimenting Teradata python module tutorial here

I am executing a query and I want to iterate over the result set several times. The problem is that if i iterate over the result set once, I can't do it again. Looks like the result set is exhausted and no more available for any further computation.

Please see the below code for details and suggest how can I preserve the result set.

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")
Ali
  • 6,852
  • 9
  • 35
  • 59

1 Answers1

2

Generators can only be iterated over once, use list or tuple to convert them, and then you can iterate over it multiple times:

rows = tuple(db.func1())

for row in rows:
    # do something

for row in rows: # would work
    # do something else
noteness
  • 2,010
  • 12
  • 14