0
点赞
收藏
分享

微信扫一扫

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()

 

举报

相关推荐

0 条评论