首页 > 解决方案 > TypeError: list indices must be integers, not RowProxy

问题描述

I make query like this in a function:

def get_artist_data(...):
    sql = 'select * from artist where id in (' + ','.join(map(str, artist_ids)) + ')'
    db_value = db.session.execute(sql).fetchall()

This is how db_value looks like:

[(121", u'', 55, u'John Lennon', datetime.date(1940, 9, 9),...), (123, u'', 23, u'Paul McCartney', datetime.date(1942, 6, 17),...)]

I want to loop through db_value:

for i in db_value:
    yield {"artist_id": db_value[i][0],
           "artist_name": db_value[i][3]}

This is what I get:

TypeError: list indices must be integers, not RowProxy

标签: pythonlist

解决方案


也许:

for i in range(0,len(db_value)-1,1):
    yield {"artist_id": db_value[i][0],
       "artist_name": db_value[i][3]}

推荐阅读