您的位置:首頁技術文章
文章詳情頁

Python flask框架實現查詢數據庫并顯示數據

瀏覽:4日期:2022-07-23 08:31:15

首先數據庫長這樣

Python flask框架實現查詢數據庫并顯示數據

我們想將name和age列顯示到web頁面

上代碼sqlshowweb.py

from flask import Flaskfrom flask import render_templateimport pymysqlapp = Flask(__name__)@app.route(’/’)def index(): conn = pymysql.connect(host=’39.106.168.84’, user=’flask_topvj_net’, password=’xxxxxxxx’, port=3306, db=’flask_topvj_net’) cur = conn.cursor() sql = 'SELECT `name`, `age` FROM `student` WHERE 1' cur.execute(sql) u = cur.fetchall() conn.close() return render_template(’index.html’,u=u)if __name__ == ’__main__’: app.debug = True app.run(port=8003)

index.html

<!DOCTYPE html><html lang='en'><head> <meta charset='UTF-8'> <title>Title</title></head><body> <table class='table table-bordered'> <tr> <th>name</th> <th>age</th> </tr> {% for i in u %} <tr><td>{{ i[0] }}</td><td>{{ i[1] }}</td> </tr> {% endfor %} </table></body></html>

運行結果

Python flask框架實現查詢數據庫并顯示數據

代碼在git上https://github.com/qingnvsue/flask的sql文件夾

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。

標簽: Python 編程
相關文章:
国产综合久久一区二区三区