2009-10-02 44 views
2

如何知道使用Query接口或GqlQuery接口的查詢結果是否返回零結果?在零結果上使用.get()會產生錯誤?如果是,那麼處理它的最好方法是什麼?Query/GqlQuery中的零結果

回答

5

做一個get()如果沒有結果,你將有一個包含對象無時

我通常做

result = query.get() 
if result is None: 
    #do the following 

,或者如果你要檢查它不是沒有那麼

if result is not None: 
    #do the following 
2

如果查詢返回沒有結果,fetch()返回空列表[]get()返回None

無論哪種情況,您都可以使用以下內容:

if result: 
    #handle the result 
else: 
    #no results were returned