python web 加数据库交互实例

# encoding: utf-8
import pymysql
import web

urls = (
    '/index/(.*)', 'index',
    '/hello/(.*)', 'hello'
)

app = web.application(urls, globals())

class hello:
    def GET(self, name):
        print name
        restName = mysqldblj(name)[0].decode('utf-8')
        return restName.decode('utf-8').encode('gb18030')


class index:
    def GET(self, name):
        print 'index get value '+ name
        web.header('Content-Type', 'text/html;charset=UTF-8')
        return 'hello' + name + u'欢迎你哟!'


def mysqldblj(id):
    db = pymysql.connect('127.0.0.1', 'root', 'admin', 'sys')
    cursor = db.cursor()
    cursor.execute("select * from testuser where id =%s", (id))
    datas = [];
    for i in cursor:
        print i[1]
        datas.append(i[1] + "--" + i[0] + "--" + i[2])

    db.close()
    cursor.close()
    return datas

if __name__ == "__main__":
    app.run()

猜你喜欢

转载自blog.csdn.net/weixin_42749765/article/details/81563814
今日推荐