简体   繁体   中英

print result as hash in sqlalchemy

sql = select id,name from table
res = session.execute(sql)
for row in res:
    print row

this code print result of execute sql( without names) as array. How to print result as hash ? (with id,name)

Try this:

columns = ('id', 'name')

sql = 'select %s from table' % ','.join(columns)
res = session.execute(sql)
for row in res:
    rowDict = dict(zip(columns, row))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM