python MySQLdb 连接数据的使用
目录
1.文件结构2.实验效果3.主文件:main.py4.base.html文件5.update.html文件6.delete.html文件7.search.html文件1.文件结构
MySQLdb和pymysql的使用差不多阅读的小伙伴可以自己尝试实现
2.实验效果
3.主文件:main.py
import MySQLdb from flask_wtf import FlaskForm from wtforms.validators import DataRequired,EqualTo,Length from wtforms import StringField,SubmitField,PasswordField,TelField from flask import Flask,render_template,redirect,url_for,abort,request,jsonify app=Flask(__name__) app.secret_key="stu" #连接数据mysql conn=MySQLdb.connect( host="127.0.0.1", port=3306, user="root", password="root", db="main" ) cur=conn.cursor() #增加用户表单 class StuForm(FlaskForm): name=StringField(label="用户名: ",validators=[DataRequired()]) password=PasswordField(label="密码: ",validators=[DataRequired(),Length(min=3,max=8)]) submit=SubmitField(label="提交") #搜索用户表单 class SStuForm(FlaskForm): name = StringField(label="用户名: ", validators=[DataRequired()]) submit=SubmitField(label="提交") #更新用户表单 class UStuForm(FlaskForm): name = StringField(label="用户名: ", validators=[DataRequired()]) password = PasswordField(label="密码: ", validators=[DataRequired(), Length(min=3, max=8)]) submit = SubmitField(label="提交") #删除用户表单 class DStuForm(FlaskForm): name = StringField(label="用户名: ", validators=[DataRequired()]) submit = SubmitField(label="提交") def CreateTab(): sql="create table student(name varchar(20),password varchar(30))" cur.execute(sql) conn.commit() cur.close() @app.route("/add",methods=["POST","GET"]) def add(): stuform=StuForm() if request.method=="POST": if stuform.validate_on_submit(): name=stuform.name.data password=stuform.password.data print("name: {}".format(name)) print("password: {}".format(password)) sql=f"insert into student(name,password) values("{name}","{password}")" cur.execute(sql) conn.commit() return jsonify("Add Successed!") return render_template("add.html",stuform=stuform) @app.route("/search",methods=["POST","GET"]) def search(): sstuform=SStuForm() if request.method=="POST": if sstuform.validate_on_submit(): name=sstuform.name.data sql=f"select count(name) from student where name="{name}"" cur.execute(sql) conn.commit() count=cur.fetchone()[0] if count<=0: return jsonify("The User is not exist!") else: sql=f"select name,password from student where name="{name}"" cur.execute(sql) conn.commit() result=cur.fetchall() return jsonify(result) return render_template("search.html",sstuform=sstuform) @app.route("/update",methods=["POST","GET"]) def update(): ustuform=UStuForm() if request.method=="POST": if ustuform.validate_on_submit(): name = ustuform.name.data password=ustuform.password.data sql = f"select count(name) from student where name="{name}"" cur.execute(sql) conn.commit() count = cur.fetchone()[0] if count <= 0: return jsonify("The User is not exist!") else: sql = f"update student set name="{name}",password="{password}" where name="{name}"" cur.execute(sql) conn.commit() return jsonify("Update Successed!") return render_template("update.html",ustuform=ustuform) @app.route("/delete",methods=["POST","GET"]) def delete(): dstuform=DStuForm() if request.method=="POST": if dstuform.validate_on_submit(): name=dstuform.name.data sql = f"select count(name) from student where name="{name}"" cur.execute(sql) conn.commit() count = cur.fetchone()[0] if count <= 0: return jsonify("The User is not exist!") else: sql=f"delete from student where name="{name}"" cur.execute(sql) conn.commit() return jsonify("Delete Successed!") return render_template("delete.html",dstuform=dstuform) @app.route("/function",methods=["POST","GET"]) def function(): if request.method=="POST": submit1 = request.form.get("submit1") submit2 = request.form.get("submit2") submit3 = request.form.get("submit3") submit4 = request.form.get("submit4") print("submit1: {}".format(submit1)) print("submit2: {}".format(submit2)) print("submit3: {}".format(submit3)) print("submit4: {}".format(submit4)) if submit1 is not None: return redirect(url_for("add")) if submit2 is not None: return redirect(url_for("search")) if submit3 is not None: return redirect(url_for("update")) if submit4 is not None: return redirect(url_for("delete")) return render_template("base.html") if __name__ == "__main__": print("Pycharm") # CreateTab() app.run(debug=True)
4.base.html文件
Title 功能选择
5.update.html文件
Insert
6.delete.html文件
Insert
7.search.html文件
Insert
到此这篇关于MySQ Ldb 连接数据的使用的文章就介绍到这了,更多相关MySQLdb连接数据内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
X 关闭
X 关闭
- 15G资费不大降!三大运营商谁提供的5G网速最快?中国信通院给出答案
- 2联想拯救者Y70发布最新预告:售价2970元起 迄今最便宜的骁龙8+旗舰
- 3亚马逊开始大规模推广掌纹支付技术 顾客可使用“挥手付”结账
- 4现代和起亚上半年出口20万辆新能源汽车同比增长30.6%
- 5如何让居民5分钟使用到各种设施?沙特“线性城市”来了
- 6AMD实现连续8个季度的增长 季度营收首次突破60亿美元利润更是翻倍
- 7转转集团发布2022年二季度手机行情报告:二手市场“飘香”
- 8充电宝100Wh等于多少毫安?铁路旅客禁止、限制携带和托运物品目录
- 9好消息!京东与腾讯续签三年战略合作协议 加强技术创新与供应链服务
- 10名创优品拟通过香港IPO全球发售4100万股 全球发售所得款项有什么用处?