flask+pymysql實(shí)現(xiàn)Web端操作數(shù)據(jù)庫的項(xiàng)目實(shí)踐
Flask是一個(gè)使用 Python 編寫的輕量級 Web 應(yīng)用框架。其 WSGI 工具箱采用 Werkzeug ,模板引擎則使用 Jinja2 。Flask使用 BSD 授權(quán)。Flask也被稱為 “microframework” ,因?yàn)樗褂煤唵蔚暮诵?,?extension 增加其他功能。Flask沒有默認(rèn)使用的數(shù)據(jù)庫、窗體驗(yàn)證工具。
PyMySQL 是在 Python3.x 版本中用于連接 MySQL 服務(wù)器的一個(gè)庫,Python2 中則使用 mysqldb。
PyMySQL 遵循 Python 數(shù)據(jù)庫 API v2.0 規(guī)范,并包含了 pure-Python MySQL 客戶端庫。
一.環(huán)境配置
flask庫和pymysql庫使用pip接口進(jìn)行安裝:
pip install flaskpip install pymysql
pip接口詳細(xì)說明可以看:http://www.dbjr.com.cn/article/257081.htm
二.實(shí)際應(yīng)用
1.首先先實(shí)現(xiàn)mysql的主程序,新建xxx.py文件:
import pymysql class Mysql(object): def __init__(self): try: self.db = pymysql.connect(host="localhost",user="root",password="密碼",database="數(shù)據(jù)庫名稱") #游標(biāo)對象 self.cursor = self.db.cursor() print("連接成功!") except: print("連接失??!") # 查詢數(shù)據(jù)函數(shù) def getdata(self): sql = "select * from 表名" #執(zhí)行sql語句 self.cursor.execute(sql) #獲取所有的記錄 results = self.cursor.fetchall() return results #關(guān)閉 def __del__(self): self.db.close()
2.然后新建另一個(gè)xxx.py文件,運(yùn)行flask框架,調(diào)用編好的html,實(shí)現(xiàn)web端輸出數(shù)據(jù)庫表內(nèi)容。
flask文件
from flask import Flask,render_template,request app = Flask(__name__) @app.route("/select",methods=['GET','POST']) def select(): #調(diào)用 db = Mysql() results = db.getdata() return render_template("select.html",results=results) if __name__ == "__main__": app.run(app.run(debug=True,port=5000,host='127.0.0.1'))
html文件
<body> <div> <h4>查詢數(shù)據(jù)</h4> <table border="1" width="30%" weight="30%"> <thead> <tr> <th>id</th> <th>worknumber</th> <th>name</th> <th>gender</th> <th>age</th> <th>idcard</th> <th>entrydate</th> </tr> </thead> <tbody> {% for result in results %} <tr> <td>{{ result[0]}}</td> <td>{{ result[1]}}</td> <td>{{ result[2]}}</td> <td>{{ result[3]}}</td> <td>{{ result[4]}}</td> <td>{{ result[5]}}</td> <td>{{ result[6]}}</td> <td><a href="/delete?id={{ result[0] }}" rel="external nofollow" ><button>刪除</button></a></td> <td><a href="/submit_insert" rel="external nofollow" ><button>插入</button></a></td> <td><a href="/submit_update" rel="external nofollow" ><button>修改</button></a></td> </tr> {% endfor %} </tbody> </table> </div>
3.運(yùn)行flask文件,網(wǎng)頁輸入http://127.0.0.1:5000/select
輸出:
三.總結(jié)
刪除、插入、修改等操作同查詢一樣,這邊就不放代碼了!
到此這篇關(guān)于flask+pymysql實(shí)現(xiàn)Web端操作數(shù)據(jù)庫的項(xiàng)目實(shí)踐的文章就介紹到這了,更多相關(guān)flask pymysql操作數(shù)據(jù)庫內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
導(dǎo)入pytorch時(shí)libmkl_intel_lp64.so找不到問題解決
這篇文章主要為大家介紹了導(dǎo)入pytorch時(shí)libmkl_intel_lp64.so找不到問題解決示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06Django?REST?Framework?(DRF)?項(xiàng)目中實(shí)現(xiàn)JWT的示例代碼
本文主要介紹了Django?REST?Framework?(DRF)?項(xiàng)目中實(shí)現(xiàn)JWT的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2025-02-02Python *args和**kwargs用法實(shí)例解析
這篇文章主要介紹了Python *args和**kwargs用法解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-03-03淺談tensorflow與pytorch的相互轉(zhuǎn)換
本文主要介紹了簡單介紹一下tensorflow與pytorch的相互轉(zhuǎn)換,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06Python如何使用Selenium WebDriver模擬用戶操作
這篇文章主要為大家詳細(xì)介紹了如何使用Selenium WebDriver來模擬用戶操作,以規(guī)避這些驗(yàn)證機(jī)制,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解下2025-04-04Python pandas RFM模型應(yīng)用實(shí)例詳解
這篇文章主要介紹了Python pandas RFM模型應(yīng)用,結(jié)合實(shí)例形式詳細(xì)分析了pandas RFM模型的概念、原理、應(yīng)用及相關(guān)操作注意事項(xiàng),需要的朋友可以參考下2019-11-11