Frage

I am using web.py and have the following database call.

results = db.query("""SELECT users.EMAIL, credentials.STATUS
                       FROM users
                       INNER JOIN credentials
                       ON users.ACCOUNT_ID = credentials.ACCOUNT_ID
                       WHERE credentials.API_KEY=%s""" % api_key)

When I am trying to access the results I am getting all sorts of errors:

>> for i in results:
>>        print i
>> print results[0]['EMAIL']
>> print results[0]['STATUS']
IndexError: already passed 0

If I remove the print statements I get the following:

Storage {'STATUS': u'ACTIVE', 'EMAIL': u'testuser@gmail.com'}

but I can no way access results as a dictionary.

What should I do?

War es hilfreich?

Lösung

The return value of the db.query method behaves like sequence, but it's basically iterator. (To be exact, it's web.utils.IterBetter object)

Iterator in Python normally cannot go backward.

Do something with the row while you iterate it.

for row in results:
    print row['EMAIL']
    print row['STATUS']

or make results as sequence to random access it:

results = list(results)
print results[0]['EMAIL']
print results[0]['STATUS']
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top