mysqlconn-flask.py

 # -*- coding: utf-8 -*-
#coding=utf-8 import os
import mysql.connector
from flask import Flask, request, render_template app = Flask(__name__) def db():
# 注意把password设为你的root口令:
conn = mysql.connector.connect( user='root', host='127.0.0.1', database='kf', use_unicode=True) # 运行查询:
cursor = conn.cursor()
cursor.execute('select check_date,comp,urn,wsurl,amount from dc_query')
# l1 = []
d1 = {}
i = 0
for (check_date,comp,urn,wsurl,amount) in cursor:
d1[urn] = (check_date,comp,urn,wsurl,amount)
# l1.append(d1)
i += 1
#print d1 # 关闭Cursor和Connection:
cursor.close()
conn.close()
return d1 @app.route('/data',methods=['GET','POST'])
def datalist():
dd = db()
return render_template('query.html',dd = dd) @app.route('/')
def index():
return render_template('index.html') if __name__ == "__main__":
port = int(os.environ.get("PORT", 5001))
app.run(host='0.0.0.0', port=port)

query.html

 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title></title>
</head>
<body> <div>dc_query表</div><br>
<table>
<tr>
<th>时间</th>
<th>模块</th>
<th>urn</th>
<th>链接</th>
</tr>
{% for urn in dd.keys() %}
<tr>
<td>{{dd[urn][0]}}</td>
<td>{{dd[urn][1]}}</td>
<td>{{dd[urn][2]}}</td>
<td>{{dd[urn][3]}}</td>
</tr>
{% endfor %}
</table>
</body>
</html>
05-21 03:55