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