python查询数据库返回数据主要运用到flask框架,pymysql 和 json‘插件’
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import pymysql
import flask,json
server=flask.Flask(__name__)
@server.route('/index',methods=['get','post'])
def create():
db = pymysql.connect("localhost", "root", "数据库密码", "表名称")
cur = db.cursor()
sql = "SELECT * FROM EMPLOYER"
cur.execute(sql)
u = cur.fetchall()
db.close()
res={'msg':u,'msg_code':0}
return json.dumps(res,ensure_ascii=False)
server.run(port=8888,debug=True,host='localhost')