flask小demo-数据查询
生活随笔
收集整理的這篇文章主要介紹了
flask小demo-数据查询
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
mysqlconn-flask.py
1 # -*- coding: utf-8 -*- 2 #coding=utf-8 3 4 import os 5 import mysql.connector 6 from flask import Flask, request, render_template 7 8 app = Flask(__name__) 9 10 def db(): 11 # 注意把password設(shè)為你的root口令: 12 conn = mysql.connector.connect( user='root', host='127.0.0.1', database='kf', use_unicode=True) 13 14 # 運(yùn)行查詢: 15 cursor = conn.cursor() 16 cursor.execute('select check_date,comp,urn,wsurl,amount from dc_query') 17 # l1 = [] 18 d1 = {} 19 i = 0 20 for (check_date,comp,urn,wsurl,amount) in cursor: 21 d1[urn] = (check_date,comp,urn,wsurl,amount) 22 # l1.append(d1) 23 i += 1 24 #print d1 25 26 # 關(guān)閉Cursor和Connection: 27 cursor.close() 28 conn.close() 29 return d1 30 31 @app.route('/data',methods=['GET','POST']) 32 def datalist(): 33 dd = db() 34 return render_template('query.html',dd = dd) 35 36 @app.route('/') 37 def index(): 38 return render_template('index.html') 39 40 if __name__ == "__main__": 41 port = int(os.environ.get("PORT", 5001)) 42 app.run(host='0.0.0.0', port=port)query.html
1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 2 "http://www.w3.org/TR/html4/loose.dtd"> 3 <html> 4 <head> 5 <title></title> 6 </head> 7 <body> 8 9 <div>dc_query表</div><br> 10 <table> 11 <tr> 12 <th>時(shí)間</th> 13 <th>模塊</th> 14 <th>urn</th> 15 <th>鏈接</th> 16 </tr> 17 {% for urn in dd.keys() %} 18 <tr> 19 <td>{{dd[urn][0]}}</td> 20 <td>{{dd[urn][1]}}</td> 21 <td>{{dd[urn][2]}}</td> 22 <td>{{dd[urn][3]}}</td> 23 </tr> 24 {% endfor %} 25 </table> 26 </body> 27 </html>?
轉(zhuǎn)載于:https://www.cnblogs.com/iuwai/p/4359346.html
總結(jié)
以上是生活随笔為你收集整理的flask小demo-数据查询的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Rational Rose概述
- 下一篇: 富士通01018z平板电脑评测_档案扫描