2012-03-14 122 views
-4

進出口新的GAE和python太,我嘗試使用的數據存儲在腳本如下GAE Python數據存儲查詢

from google.appengine.ext import db 
from google.appengine.ext import webapp 
from google.appengine.ext.webapp import util 

class Pincodes(db.Model): 
    city = db.StringProperty() 
    code = db.StringProperty() 

class MainHandler(webapp.RequestHandler): 
    def get(self): 
    q = Pincodes.all() 
    q = q.filter("city =", "some_city") 
    p = q.get() 
    r = 'city: %s code: %s' % (pincode.city, pincode.code) 
    self.response.out.write(r)  

我的劇本還含有常用的高清main()和if__name構建簡單的應用程序,即時通訊發展它從code.google文檔中顯示簡單的Hello World應用程序一步一步,它工作正常,我hav上傳包含10條記錄本地數據存儲示範pincode數據和它的罰款,但我不能夠查詢和顯示它在網頁上我試圖自我.response.out.write和輸出是「city:code:」而不是「city:mumbai code:400001」我的腳本有什麼問題

+1

*什麼*顯示?你如何做這個查詢?你在運行什麼代碼?你在哪裏運行它? – 2012-03-14 05:54:51

+0

它的示例應用程序即時創建開發應用程序服務器,即時通訊查詢它與query.all和gql的方法,但它顯示消息我上面提到,即時通訊運行的代碼,我從演示評論的應用程序和我的gae sdk是1.6.1和python 2.5.4,即時通訊一個新的,所以你可以幫我寫一個示例腳本,我可以使用。 – armani2811 2012-03-14 16:22:37

+0

不可以。您沒有回覆我的評論。 **顯示你的代碼**。 – 2012-03-14 16:41:44

回答

1

use

entity = q.get() # use get if you want one entity only 
r = 'city: %s code: %s' %(entity.city, entity.code) 
self.response.out.write(r) 

,而不是打印

編輯:

def get(self): 
    q = Pincodes.all() 
    q = q.filter("city =", "some_city") 
    entity = q.get() # use get if you want one entity only 
    r = 'city: %s code: %s' %(entity.city, entity.code) 
    self.response.out.write(r) 

EDIT2:

def get(self): 
    q = Pincodes.all() 
    q = q.filter("city =", "some_city") 
    entity = q.get() # use get if you want one entity only 
    if not entity: 
     self.response.out.write('sorry no entities found') 
    else: 
     r = 'city: %s code: %s' %(entity.city, entity.code) 
     self.response.out.write(r) 
+0

嘗試過你的答案,但它仍然無法正常工作,查詢沒有發生。 – armani2811 2012-03-21 06:12:36

+0

我編輯了我的答案。嘗試複製粘貼代碼,看看是否給你預期的結果 – aschmid00 2012-03-21 13:03:14

+0

現在它顯示「AttributeError:'unicode'對象沒有屬性'has_key'」你有沒有任何想法是什麼意思? – armani2811 2012-03-23 04:37:33