日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

flask读取数据库(mysql)并展示表格(讲解获取表头的方法)【附上flask好看点的helloworld】

發布時間:2025/4/16 数据库 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 flask读取数据库(mysql)并展示表格(讲解获取表头的方法)【附上flask好看点的helloworld】 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

簡述

為了網頁的好看,最好還是用bootstrap

文章目錄

    • 簡述
    • 好看點的helloworld
    • 鏈接數據庫
    • 結合在html上和flask上
      • html代碼修改

好看點的helloworld

anyway,先看初始版本的 helloworld

  • /template/index.html
  • 來自于bootstrap官網
<!doctype html> <html lang="en"><head><!-- Required meta tags --><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"><!-- Bootstrap CSS --><link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous"><title>Hello, world!</title></head><body><h1>Hello, world!</h1><!-- Optional JavaScript --><!-- jQuery first, then Popper.js, then Bootstrap JS --><script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script><script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script><script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script></body> </html>
  • ./app.py
from flask import Flask, render_templateapp = Flask(__name__)@app.route('/') def hello_world():return render_template('index.html')if __name__ == '__main__':app.run()

鏈接數據庫

這里我推薦使用pymysql
因為這個在不同平臺上都可以使用,而且安裝也沒什么坑。

pip install pymysql
  • 鏈接的示范
    • conn是一個連接器
    • host是url
    • user是用戶
    • password是密碼
    • db是數據庫(也就是show databases;可以看到的)
    • charset主要是為了設置為可以看中文
import pymysqlconn = pymysql.connect(host='127.0.0.1',user='root',password='1234',db='library_management_system',charset='utf8' )

結合在html上和flask上

html代碼修改

  • /templates/index.html
<!doctype html> <html lang="en"> <head><!-- Required meta tags --><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"><!-- Bootstrap CSS --><link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css"integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous"><title>Hello, world!</title> </head> <body> <div class="row"><div class="col-md-6 col-sm-12 col-xs-12"><div class="panel panel-default"><div class="panel-heading"><h3>Students</h3></div><div class="panel-body"><div class="table-responsive"><table class="table table-striped table-bordered table-hover"><thead><tr>{% for i in labels %}<td>{{ i }}</td>{% endfor %}</tr></thead><tbody>{% for i in content %}<tr>{% for j in i %}<td>{{ j }}</td>{% endfor %}</tr>{% endfor %}</tbody></table></div></div></div></div></div> <!-- Optional JavaScript --> <!-- jQuery first, then Popper.js, then Bootstrap JS --> <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo"crossorigin="anonymous"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js"integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49"crossorigin="anonymous"></script> <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy"crossorigin="anonymous"></script> </body> </html>
  • app.py
from flask import Flask, render_template import pymysqlapp = Flask(__name__)conn = pymysql.connect(host='127.0.0.1',user='root',password='1234',db='jxgl',charset='utf8' )@app.route('/') def hello_world():cur = conn.cursor()# get annual sales ranksql = "select * from student"cur.execute(sql)content = cur.fetchall()# 獲取表頭sql = "SHOW FIELDS FROM student"cur.execute(sql)labels = cur.fetchall()labels = [l[0] for l in labels]return render_template('index.html', labels=labels, content=content)if __name__ == '__main__':app.run()
  • 效果:

總結

以上是生活随笔為你收集整理的flask读取数据库(mysql)并展示表格(讲解获取表头的方法)【附上flask好看点的helloworld】的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。