代码改变世界

python 查询数据库返回为字典

2020-09-13 14:20  idea555  阅读(1185)  评论(0)    收藏  举报
from flask import Flask,render_template
import pymysql
import json

app = Flask(__name__)

conn = pymysql.connect('localhost',user = "root",password = "123456",db = "testdb")
cursot = conn.cursor(pymysql.cursors.DictCursor)


@app.route('/')
def hello_world():
return 'Hello World!'

@app.route('/list')
def list():


cursot.execute("select * from news")
rows = cursot.fetchall()


#object_list = []
#for row in rows:
# result = {}
# result["Id"] = row[0]
# result["Name"] = row[1]
# result["Age"] = row[2]
# result["AddTime"] = row[3]
# object_list.append(result)


return render_template('show.html',res = rows)
cursot.close()
conn.close()




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