在SQLAlchemy核心中,结果为RowProxy。如果您需要显式词典,可以致电dict(row)。
首先为示例进行设置:
import datetime as dt from sqlalchemy import ( Column, Date, Integer, MetaData, Table, Text, create_engine, select) metadata = MetaData() users = Table( 'users', metadata, Column('id', Integer, primary_key=True), Column('name', Text, nullable=False), Column('birthday', Date), ) engine = create_engine('sqlite://') metadata.create_all(bind=engine) engine.execute(users.insert(), name='Alice', birthday=dt.date(1990, 1, 1))
然后从结果行创建字典:
with engine.connect() as conn: result = conn.execute(users.select()) for row in result: print(dict(row)) result = conn.execute(select([users.c.name, users.c.birthday])) for row in result: print(dict(row))