flask小demo-数据查询

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设为你的root口令:
12     conn = mysql.connector.connect( user='root', host='127.0.0.1', database='kf', use_unicode=True)
13  
14     # 运行查询:
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     # 关闭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>时间</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>
原文地址:https://www.cnblogs.com/iuwai/p/4359346.html