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. 利用promise及參數(shù)解構(gòu)封裝ajax請(qǐng)求的方法2. JSP數(shù)據(jù)交互實(shí)現(xiàn)過(guò)程解析3. windows服務(wù)器使用IIS時(shí)thinkphp搜索中文無(wú)效問(wèn)題4. .NET中l(wèi)ambda表達(dá)式合并問(wèn)題及解決方法5. Nginx+php配置文件及原理解析6. 淺談python出錯(cuò)時(shí)traceback的解讀7. ASP 信息提示函數(shù)并作返回或者轉(zhuǎn)向8. Ajax實(shí)現(xiàn)表格中信息不刷新頁(yè)面進(jìn)行更新數(shù)據(jù)9. Python importlib動(dòng)態(tài)導(dǎo)入模塊實(shí)現(xiàn)代碼10. python matplotlib:plt.scatter() 大小和顏色參數(shù)詳解
