Python flask框架實(shí)現(xiàn)查詢數(shù)據(jù)庫(kù)并顯示數(shù)據(jù)
首先數(shù)據(jù)庫(kù)長(zhǎng)這樣
我們想將name和age列顯示到web頁(yè)面
上代碼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>
運(yùn)行結(jié)果
代碼在git上https://github.com/qingnvsue/flask的sql文件夾
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. Spring security 自定義過濾器實(shí)現(xiàn)Json參數(shù)傳遞并兼容表單參數(shù)(實(shí)例代碼)2. Java8內(nèi)存模型PermGen Metaspace實(shí)例解析3. Python 的 __str__ 和 __repr__ 方法對(duì)比4. 基于python實(shí)現(xiàn)操作git過程代碼解析5. python 實(shí)現(xiàn)"神經(jīng)衰弱"翻牌游戲6. python學(xué)習(xí)之plot函數(shù)的使用教程7. 如何基于python把文字圖片寫入word文檔8. python 實(shí)現(xiàn)關(guān)聯(lián)規(guī)則算法Apriori的示例9. python wsgiref源碼解析10. 一文搞懂 parseInt()函數(shù)異常行為
