Python flask 프레임 워 크 는 데이터 베 이 스 를 조회 하고 데 이 터 를 표시 합 니 다.

일단 데이터베이스 가 이렇게 생 겼 어 요.

웹 페이지 에 name 과 age 열 을 표시 하고 싶 습 니 다.
코드 sqlshowweb.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)
index.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>
실행 결과

코드 는 git 에 있 습 니 다https://github.com/qingnvsue/flasksql 폴 더 입 니 다.
이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.

좋은 웹페이지 즐겨찾기