日期:2014-05-16  浏览次数:20385 次

python微信五数据库

sae数据库表py,字段id,pro,value,输入省份,返回省会。前期测试错误,原来是字段名为key可能为保留字。后发现不用s%,直接用sql语句也可以,注意链接utf8

# -*- coding: utf-8 -*-
import sae
import web
import xml.etree.ElementTree as ET
import sae.const
import MySQLdb

urls = (
    '/', 'Hello'
)


class Hello:

    def GET(self):
        data = web.input()
        echostr = data.echostr
        return echostr

    def POST(self):
        data = web.data()
        root = ET.fromstring(data)
        fromUser = root.findtext(".//FromUserName")
        toUser = root.findtext(".//ToUserName")
        CreateTime = root.findtext(".//CreateTime")
        MsgType = root.findtext(".//MsgType")
        Content = root.findtext(".//Content")
        Content = Content.encode('UTF-8')
        db = MySQLdb.connect(
            host=sae.const.MYSQL_HOST, port=int(sae.const.MYSQL_PORT),
            user=sae.const.MYSQL_USER, passwd=sae.const.MYSQL_PASS, db=sae.const.MYSQL_DB,charset='utf8')
        c=db.cursor()
        sql="select * from py WHERE pro='"+Content+"'" 
        c.execute(sql)   
        rows = c.fetchone()
        tpl = '''<xml>
<ToUserName>''' + fromUser + '''</ToUserName>
<FromUserName>''' + toUser + '''</FromUserName>
<CreateTime>''' + CreateTime + '''</CreateTime>
<MsgType><![CDATA[text]]></MsgType>
<Content>'''+rows[2]+'''</Content>
</xml>'''
        return tpl;

app = web.application(urls, globals()).wsgifunc()
application = sae.create_wsgi_app(app)