py文件:sqlshowweb.py
from flask import Flask
from flask import render_template
import pymysql
app = Flask(__name__)
@app.route('/test1')
def index():
conn = pymysql.connect(host='192.168.1.**', user='root', password='****', port=3306,
db='lxl-test')
cur = conn.cursor()
sql = "SELECT `id`, `userno` FROM `reply` WHERE 1"
cur.execute(sql)
u = cur.fetchall()
conn.close()
return render_template('index.html', u=u)
#return u
if __name__ == '__main__':
app.debug = True
app.run(port=8002)
html文件:(需放在templates文件夹下)
<!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>
运行sqlshowweb.py
访问http://127.0.0.1:8002/test1 得到网页效果:
nameage12222222233333333