在sqlalchemy中将结果打印为散列

时间:2011-02-11 11:23:17

标签: python sqlalchemy

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

此代码将执行sql(无名称)的结果打印为数组。如何打印结果作为哈希? (带身份证,姓名)

1 个答案:

答案 0 :(得分:0)

试试这个:

columns = ('id', 'name')

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