python 查詢數據庫返回為字典


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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM