最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
python MySQLdb连接数据使用代码示例
时间:2022-06-25 01:19:24 编辑:袖梨 来源:一聚教程网
本篇文章小编给大家分享一下python MySQLdb连接数据使用代码示例,文章代码介绍的很详细,小编觉得挺不错的,现在分享给大家供大家参考,有需要的小伙伴们可以来看看。
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 count4.base.html文件
Title 功能选择
5.update.html文件
Insert 6.delete.html文件
Insert 7.search.html文件
Insert
相关文章
- 无限暖暖神秘作者任务怎么做 神秘作者任务流程攻略 09-18
- 如鸢董奉怎么玩 董奉玩法攻略 09-18
- 嘟嘟脸恶作剧活泼队怎么搭配 活泼队搭配推荐 09-18
- 如鸢曹植值得抽吗 曹植抽取建议 09-18
- 如鸢主线9-13王允低练度怎么过 主线9-13王允低练度过关攻略 09-18
- 嘟嘟脸恶作剧薇尔薇特角色如何 09-18