/*python(数据库的增删改查) 目录结构: |-users.py |-app.yaml 根目录-| | |-index.htm |-static-|-other.htm |-images-|1.jpg 说明:方法不完全正确或者说不是最好的方法, 但是代码能正确运行. */ //user.py文件内容: # -*- coding: utf-8 -*- import os import re import cgi import datetime import wsgiref.handlers from google.appengine.ext import db from google.appengine.api import users from google.appengine.ext import webapp from google.appengine.ext.webapp import template class UserInfo(db.Model): name=db.StringProperty() pwd=db.StringProperty() adddate=db.DateTimeProperty(auto_now_add=True) count = db.IntegerProperty() class MainPage(webapp.RequestHandler): def get(self): Users = db.GqlQuery("SELECT * FROM UserInfo ORDER BY adddate DESC LIMIT 10") if Users: for user in Users: self.response.out.write('%s,%s,%s'%(user.name,user.adddate,user.count)) self.response.out.write("<br />") else: self.response.out.write('no records') self.response.out.write(""" <form action="/add" method="post"> <div>name<input type="text" name="name"/></div> <div>pwd<input type="text" name="pwd"/></div> <div><input type="submit" value="add"></div> </form> <form action="/delete" method="post"> <div><input type="submit" value="delete"></div> </form> <form action="/update" method="post"> <div><input type="submit" value="update"></div> </form> </body> </html>""") class Add(webapp.RequestHandler): def post(self): usr = UserInfo() usr.name = self.request.get('name') usr.pwd = self.request.get('pwd') usr.count = 2 usr.put() self.redirect('/dynamic') class Delete(webapp.RequestHandler): def post(self): DelUser = db.GqlQuery("SELECT * FROM UserInfo ORDER BY adddate DESC LIMIT 10") if DelUser: for dUser in DelUser: dUser.delete() self.redirect('/dynamic') class Update(webapp.RequestHandler): def post(self): upUser = db.GqlQuery("SELECT * FROM UserInfo WHERE count = :1",2) if upUser: for upUser in upUser: upUser.name='tree2009' db.put(upUser) self.redirect('/dynamic') def main(): application = webapp.WSGIApplication([('/dynamic', MainPage),('/add', Add),('/delete',Delete),('/update',Update)],debug=True) wsgiref.handlers.CGIHandler().run(application) if __name__ == "__main__": main() //app.yaml文件内容: application: greatverve version: 1 runtime: python api_version: 1 handlers: - url: /dynamic script: users.py - url: /add script: users.py - url: /delete script: users.py - url: /update script: users.py - url: /login script: users.py - url: / static_files: static/index.htm upload: static/index.htm - url: /static static_dir: static - url: /.+ script: main.py //submit.htm文件内容: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head> <title>提交</title> <mce:script type="text/javascript"><!-- window.οnlοad=function(){ document.getElementById("i1").οnclick=function(){ document.forms[0].action="/add"; document.forms[0].submit(); } document.getElementById("i2").οnclick=function(){ document.forms[0].action="action2.htm"; document.forms[0].submit(); } } // --></mce:script> </head> <body> <form id="form1" action="action1.htm"> <div>name<input type="text" name="name"/></div> <div>pwd<input type="text" name="pwd"/></div> <input id="i1" type="button" value="action1" /> <input id="i2" type="button" value="action2" /> </form> </body> </html>