0

我的 SQLAlchemy 代码:

used_emails = list(db.execute(halo4.select(halo4.c.email != '')))

返回:

[(80940L, 'P7J4H-DFDMG-G6HMJ-W7PF9-MDTF1', '1', 'first.last@domain.com', datetime.datetime(2012, 11, 3, 3, 48, 58)), (80939L, 'GCTFY-QPK2Y-PX1CJ-W69QY-PHDGZ', '1', 'first.last@domain.com', datetime.datetime(2012, 11, 3, 15, 8, 15)), (80938L, 'R4XGV-PG461-RGXX7-9R47R-2RWYZ', '1', 'first.last@domain.com', datetime.datetime(2012, 11, 3, 15, 8, 27))]

如何让 SQLAlchemy 只返回:

[('first.last@domain.com', 'first.last@domain.com', 'first.last@domain.com')]

4

1 回答 1

0

下面将给出email列值的列表:

from sqlalchemy.sql.expression import select

used_emails = [row.email for row
               in db.execute(select([halo4.c.email], halo4.c.email!='')]

基本上你是在告诉函数只为列select()生成一个语句。的结果将是 a所以列表推导的工作是从从 iterable 获得的单列行创建列字符串值列表。SELECTemailexecute()ResultProxyemailResultProxy

于 2012-11-04T17:35:54.517 回答