문제

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?

도움이 되었습니까?

해결책

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']
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top