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 폴 더 입 니 다.
이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Python의 None과 NULL의 차이점 상세 정보그래서 대상 = 속성 + 방법 (사실 방법도 하나의 속성, 데이터 속성과 구별되는 호출 가능한 속성 같은 속성과 방법을 가진 대상을 클래스, 즉 Classl로 분류할 수 있다.클래스는 하나의 청사진과 같아서 하나의 ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.