12
import re
from sqlalchemy import *

db = create_engine('sqlite:///code.db')

db.echo = True

metadata = MetaData(db)

halo4 = Table('emblem_codes', metadata, autoload=True)

#########################
## Get One Unused Code ##
#########################

s = select([halo4.c.code, halo4.c.status=='None'])
result = s.execute()

for row in result:
    print row

我的代码返回所有 80999 结果。我只想要一个。最好的方法是什么?

这是 code.db 的示例:

(u'JRQRY-MYHW3-D67GR-7YWFF-CRJ31', None)
(u'9D7DR-4WFDY-VG49F-3DYCG-7DYT5', None)
(u'QR9WT-P3CTM-PW4WW-34JJ4-RFKV7', None)
(u'7FMXQ-H97TC-FFYC6-XHXFV-Y7KR2', None)
(u'VKHXW-29WC4-4PF4Y-2QMJ4-4W2H4', None)
4

1 回答 1

22

如果您只想从ResultProxy对象中获取一行(这是您的s.execute()语句的结果,您需要使用以下fetchone()方法:

s = select([halo4.c.code, halo4.c.status=='None'])
result = s.execute()
one_row = result.fetchone()
another_row = result.fetchone()
于 2012-11-03T20:55:29.633 回答