我正在尝试在 App Engine 上构建一个网络应用程序,在某个时刻,我需要一个函数来构建一个列表属性,其中包含来自不同组的一个成员。如果我使用 Sqlite 数据库和 web.py 数据库模块进行操作,我可以让它工作,我可以像这样实现我需要的东西:
db = web.database(dbn='sqlite',db=':memory:')
db.query("CREATE TABLE seq_list (seq TEXT, seq_pickle TEXT);")
# do some stuff to the database
def getUnique():
uniq_entries = db.query("SELECT DISTINCT seq FROM seq_list;")
if not uniq_sequences:
for entry in uniq_entries:
seq_query = db.query("SELECT * FROM seq_list WHERE seq='" + str(entry.seq) +"';")
seq_obj = pickle.loads(seq_query[0].seq_pickle)
self.uniq_sequences.append(seq_obj)
不过,我无法让它在 App 引擎中工作。
class SeqObj(db.Model):
seq = db.TextProperty(required = True)
seq_pickle = db.TextProperty(required = True)
# do some stuff to the database
def getUnique():
entries = db.GqlQuery("SELECT * FROM SeqObj")
entries = list(entries)
if not uniq_sequences:
unique_entries = set([entry.seq for entry in entries])
# this prints the unique entries correctly
print unique_entries
for entry in unique_entries:
try:
q = "SELECT * FROM SeqObj WHERE seq='%s'"% entry
print q
seq_query = db.GqlQuery(q)
# these are the lines that break it
seq_obj = pickle.loads(seq_query[0].seq_pickle)
uniq_sequences.append(seq_obj)
print entry
except:
pass
try/except 用于显示打印语句。这会输出一些测试数据:
set([u'DXTMT', u'DIXTX', u'XSXDV', u'XI*MT'])
SELECT * FROM SeqObj WHERE seq='DXTMT'
SELECT * FROM SeqObj WHERE seq='DIXTX'
SELECT * FROM SeqObj WHERE seq='XSXDV'
SELECT * FROM SeqObj WHERE seq='XI*MT'
当我没有把所有的东西都放在 try catch 块中时,我得到了
IndexError: The query returned fewer than 1 results
我不知道出了什么问题,因为我返回了唯一的序列,查询看起来也像我期望的那样,并且我可以验证条目是否在数据存储中。任何帮助表示赞赏。