q = db.GqlQuery("SELECT * FROM Message WHERE create_date < :1", earliest_date)
results = q.fetch(10)
for result in results:
result.delete()
# or...
q = db.GqlQuery("SELECT * FROM Message WHERE create_date < :1", earliest_date)
results = q.fetch(10)
db.delete(results)
但し、GQLには以下の制限があって、保存してあるデータ量が多いと消えない可能性がある。
- 一定時間内にリクエストを返すべし
- 一度に取り出せる量が1000件まで
- 使えるリソースの上限を超えた
getMyData, getMyURLを自分のGAEデータモデルに変更すること。
from google.appengine.ext import webapp from google.appengine.ext import db from google.appengine.ext.webapp.util import run_wsgi_app class MyData1(db.Model): pass class MyData2(db.Model): pass class AbstractRemoveAll(webapp.RequestHandler): def get(self) : #limit = 50 limit = 100 list = self.getMyList(limit) if list==None : self.response.headers['Content-Type'] = 'text/plain' self.response.out.write( 'Error.' ) return if len(list)<1: self.response.headers['Content-Type'] = 'text/plain' self.response.out.write( 'OK remove all data.' ) return for c in list: c.delete() self.response.headers['Content-Type'] = 'text/html' self.response.out.write( '<html>' ) self.response.out.write( '<head>' ) self.response.out.write( '<META HTTP-EQUIV="REFRESH" CONTENT="10;URL='+self.getMyUrl()+'">') self.response.out.write( '</head>' ) self.response.out.write( '<body>' ) self.response.out.write( '<p>' ) self.response.out.write( len(list) ) self.response.out.write( '</p>' ) self.response.out.write( 'Again after 10seconds.' ) self.response.out.write( '</body>' ) self.response.out.write( '</html>' ) def getMyUrl(self) : return '/RemoveAll' def getMyList(self,limit): return [] class RemoveAll1(AbstractRemoveAll): def getMyUrl(self) : return '/RemoveAll1' def getMyList(self,limit): q=MyData1.gql('LIMIT '+str(limit)) return q[0:min(q.count(),limit)] class RemoveAll2(AbstractRemoveAll): def getMyUrl(self) : return '/RemoveAll2' def getMyList(self,limit): q=MyData2.gql('LIMIT '+str(limit)) return q[0:min(q.count(),limit)] application = webapp.WSGIApplication( [ ('/RemoveAll2', RemoveAllPage2), ('/RemoveAll1', RemoveAllPage1) ], debug=True) def main(): run_wsgi_app(application) if __name__ == "__main__": main()
0 件のコメント:
コメントを投稿