python在前端页面使用 MySQLdb 连接数据
作者:Keep_Trying_Go 时间:2024-01-21 07:30:09
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文件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
.h1{
position:relative;
margin:auto;
width:500px;
height:50px;
margin-top:100px;
margin-left:650px;
}
.form {
position:relative;
width:500px;
height:50px;
margin:auto;
margin-top:50px;
border:2px solid #000000;
color:#000000;
font-size:20px;
font-weight:400;
}
.form1{
position:absolute;
margin-top:10px;
margin-left:80px;
}
.form2{
position:absolute;
margin-top:10px;
margin-left:180px;
}
.form3{
position:absolute;
margin-top:10px;
margin-left:280px;
}
.form4{
position:absolute;
margin-top:10px;
margin-left:380px;
}
</style>
</head>
<body>
<div class="h1">
<h1>功能选择</h1>
</div>
<div class="form">
<form class="form1" action="http://127.0.0.1:5000/add" method="POST">
<input type="submit" name="submit1" value="添加">
</form>
<form class="form2" action="http://127.0.0.1:5000/search" method="POST">
<input type="submit" name="submit2" value="搜索">
</form>
<form class="form3" action="http://127.0.0.1:5000/update" method="POST">
<input type="submit" name="submit3" value="更新">
</form>
<form class="form4" action="http://127.0.0.1:5000/delete" method="POST">
<input type="submit" name="submit4" value="删除">
</form>
</div>
</body>
</html>
5.update.html文件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Insert</title>
<style>
div{
width:255px;
height:100px;
margin:auto;
margin-top:200px;
border:2px solid #000000;
font-size:20px;
font-weight:400px;
background:#FFFFFF;
}
.submit{
margin-top:10px;
margin-left:100px;
}
</style>
</head>
<body>
<div>
<form action="" method="POST">
{{ustuform.csrf_token()}}
{{ustuform.name.label}}{{ustuform.name}}<br>
{{ustuform.password.label}}{{ustuform.password}}<br>
<input class="submit" type="submit" name="submit" value="更新">
</form>
</div>
</body>
</html>
6.delete.html文件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Insert</title>
<style>
div{
width:255px;
height:100px;
margin:auto;
margin-top:200px;
border:2px solid #000000;
font-size:20px;
font-weight:400px;
background:#FFFFFF;
}
.submit{
margin-top:10px;
margin-left:100px;
}
</style>
</head>
<body>
<div>
<form action="" method="POST">
{{dstuform.csrf_token()}}
{{dstuform.name.label}}{{dstuform.name}}<br>
<input class="submit" type="submit" name="submit" value="删除">
</form>
</div>
</body>
</html>
7.search.html文件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Insert</title>
<style>
div{
width:255px;
height:100px;
margin:auto;
margin-top:200px;
border:2px solid #000000;
font-size:20px;
font-weight:400px;
background:#FFFFFF;
}
.submit{
margin-top:10px;
margin-left:100px;
}
</style>
</head>
<body>
<div>
<form action="" method="POST">
{{sstuform.csrf_token()}}
{{sstuform.name.label}}{{sstuform.name}}<br>
<input class="submit" type="submit" name="submit" value="搜索">
</form>
</div>
</body>
</html>
来源:https://blog.csdn.net/Keep_Trying_Go/article/details/123695827
标签:python,MySQLdb,连接,数据
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python实现无损放大图片的示例代码
2022-12-15 12:50:12
![](https://img.aspxhome.com/file/2023/7/114677_0s.jpg)
Python中Django框架利用url来控制登录的方法
2022-03-27 18:15:37
Python3.5面向对象编程图文与实例详解
2023-12-04 23:21:51
![](https://img.aspxhome.com/file/2023/8/88008_0s.png)
又一个仿alert提示效果
2007-12-24 17:34:00
![](https://img.aspxhome.com/file/UploadPic/200712/24/20071224173935223s.jpg)
按键标示的设计体验
2008-08-27 12:06:00
![](https://img.aspxhome.com/file/UploadPic/20088/27/2008827121557448s.jpg)
如何进行MySQL数据库表的故障检测
2009-02-10 10:34:00
python Jupyter运行时间实例过程解析
2022-04-15 04:38:21
Python手动或自动协程操作方法解析
2023-06-30 11:38:41
Python线程编程之Thread详解
2022-04-10 17:37:01
![](https://img.aspxhome.com/file/2023/7/123527_0s.png)
静态网页加密工具
2009-01-05 12:05:00
使用Python写一个量化股票提醒系统
2022-04-19 14:35:42
![](https://img.aspxhome.com/file/2023/0/65890_0s.jpg)
Python爬虫 bilibili视频弹幕提取过程详解
2023-07-05 06:41:25
![](https://img.aspxhome.com/file/2023/7/101567_0s.png)
浅谈Python在pycharm中的调试(debug)
2023-05-04 15:33:20
![](https://img.aspxhome.com/file/2023/1/125021_0s.jpg)
Python实现MySql数据库交互的示例
2024-01-23 02:41:13
![](https://img.aspxhome.com/file/2023/5/97275_0s.png)
word-wrap同word-break的区别
2007-10-24 20:08:00
jupyter notebook更换皮肤主题的实现
2023-04-13 07:31:38
![](https://img.aspxhome.com/file/2023/9/131559_0s.jpg)
Python如何使用带有 for 循环的 Lambda 函数
2021-05-28 05:06:05
python opencv3实现人脸识别(windows)
2023-11-09 11:21:17
利用Python的folium包绘制城市道路图的实现示例
2021-01-26 02:02:16
![](https://img.aspxhome.com/file/2023/4/82794_0s.jpg)
python调用c++传递数组的实例
2021-09-23 03:30:47