Flask 操作数据库

2020-07-01  本文已影响0人  JalorOo

原著网址:https://www.jb51.net/article/188010.htm
main.py

from flask import Flask
from flask import render_template
import pymysql
 
app = 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)

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>
上一篇 下一篇

猜你喜欢

热点阅读