iis服务器助手广告广告
返回顶部
首页 > 资讯 > 数据库 >python在前端页面使用 MySQLdb 连接数据
  • 561
分享到

python在前端页面使用 MySQLdb 连接数据

2024-04-02 19:04:59 561人浏览 薄情痞子
摘要

目录1.文件结构2.实验效果3.主文件:main.py4.base.html文件5.update.html文件6.delete.html文件7.search.html文件1

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>

到此这篇关于MySQ Ldb 连接数据的使用的文章就介绍到这了,更多相关MySQLdb连接数据内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

您可能感兴趣的文档:

--结束END--

本文标题: python在前端页面使用 MySQLdb 连接数据

本文链接: https://www.lsjlt.com/news/143780.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • python在前端页面使用 MySQLdb 连接数据
    目录1.文件结构2.实验效果3.主文件:main.py4.base.html文件5.update.html文件6.delete.html文件7.search.html文件1...
    99+
    2022-11-13
  • python中MySQLdb连接数据怎么用
    这篇文章主要介绍了python中MySQLdb连接数据怎么用,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。1.文件结构MySQLdb和pymysql的使用差不多阅读的小伙伴可...
    99+
    2023-06-29
  • python中SQLAlchemy使用前端页面实现插入数据
    目录1.实验效果2.主main.py文件3.前端mysql.html文件1.实验效果 如果插入的数据已经存在于数据库中,则出现以下提示: 查看数据库表中的数据,发现已经将数据存...
    99+
    2022-11-13
  • python中SQLAlchemy怎么使用前端页面实现插入数据
    这篇文章主要介绍“python中SQLAlchemy怎么使用前端页面实现插入数据”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“python中SQLAlchemy怎么使用前端页面实现插入数据”文章能帮...
    99+
    2023-06-29
  • 如何使用easyui从servlet传递json数据到前端页面
    这篇文章给大家分享的是有关如何使用easyui从servlet传递json数据到前端页面的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。 两种方法获取的数据在servle...
    99+
    2022-10-19
  • 怎么运用layui数据添加页面的前端布局
    这篇文章主要介绍怎么运用layui数据添加页面的前端布局,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!layui数据添加页面的前端布局1、在首页面增加一个学生信息添加,代码如下:<title>学生信息添加...
    99+
    2023-06-15
  • 使用Python统计端口TCP连接数
        此脚本可以用来统计某个端口上连接的IP的数量,统计连接到这一端口的所有IP、最多的IP和次数以及TCP连接状态。    涉及到Python读取网络连接统计信息以及统计计算的一些基本操作。在编写脚本的过程中预先定义了统计信息的数据结构...
    99+
    2023-01-31
    端口 连接数 Python
  • vue项目使用node连接数据库的方法(前后端分离)
    目录写在前面编写顺序开始2.制作后端 node 服务器3.编写 node 连接数据库4.实现登录、修改密码和获取用户信息接口5.完成前后端交互结束学习关键语句:vue连接mysql数...
    99+
    2022-12-22
    vue使用node连接数据库 vue前后端分离项目
  • 怎么在python中使用socket连接客户端
    本篇文章给大家分享的是有关怎么在python中使用socket连接客户端,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。Python的优点有哪些1、简单易用,与C/C++、Jav...
    99+
    2023-06-14
  • 使用instantclient客户端连接oracle数据库
    (1)首先下载instantclient_11_2 (解压如下) 修改instantclient_11_2/tnsnames.ora 文件,将你需要链接的oracle服务器地址写上...
    99+
    2022-11-13
  • dedecms使用getall获取当前页面tag标签(超连接)的方法
    本文实例讲述了dedecms使用getall获取当前页面tag标签(超连接)的方法。分享给大家供大家参考。具体分析如下: 关于dedecms中tag标签我这两天接触最多了,昨天发现无法实现调用当前页面的tag标签,而调用...
    99+
    2022-06-12
    dedecms getall 获取 当前页面 tag 标签 超连接 方法
  • 使用python连接mysql数据库数据方式
    目录1.fetchone/fetchmany/fetchall2.pandas.read_sql()前言: 使用python连接mysql数据库数据 有以下两种读取数据的方式推荐: ...
    99+
    2022-11-13
  • django将图片保存到mysql数据库并展示在前端页面的实现
    小编使用python中的django框架来完成! 1,首先用pycharm创建django项目并配置相关环境 这里小编默认项目都会创建 settings.py中要修改的两处配置 ...
    99+
    2022-11-12
  • 如何使用vue+node作后端连接数据库
    这篇文章主要介绍了如何使用vue+node作后端连接数据库的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇如何使用vue+node作后端连接数据库文章都会有所收获,下面我们一起来看看吧。1、编写node服务器先下...
    99+
    2023-07-05
  • 如何使用instantclient客户端连接oracle数据库
    本篇内容介绍了“如何使用instantclient客户端连接oracle数据库”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!(1)首先下载i...
    99+
    2023-06-30
  • 使用python把Excel中的数据在页面中可视化
    目录一. 需求二. 安装xlrd模块三.  用echart在html中表现总结一. 需求 最近我们数据可视化的老师让我们把广州历史房价中的房价数据可视化,然后给我们发了广州...
    99+
    2022-11-13
  • 将图片保存到mysql数据库并展示在前端页面的实现代码
    目录1,首先用pycharm创建django项目并配置相关环境2,创建表3,上传图片功能4,展示图片功能5,删除图片功能小编使用python中的django框架来完成! 1,首先用p...
    99+
    2022-11-12
  • 怎么在python中使用pymysql模块连接mysql数据库
    本篇文章给大家分享的是有关怎么在python中使用pymysql模块连接mysql数据库,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。安装py...
    99+
    2022-10-18
  • 如何使用python连接mysql数据库数据方式
    这篇文章将为大家详细讲解有关如何使用python连接mysql数据库数据方式,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。前言:使用python连接mysql数据库数据有以下两种读取数据的方式推荐:一种是...
    99+
    2023-06-29
  • python怎么使用mysql数据库连接池
    python使用mysql数据库连接池的方法:安装数据库连接池模块DBUtils。pip3 install DBUtilsDBUtils是一套Python数据库连接池包,并允许对非线程安全的数据库接口进行线程安全包装。下...
    99+
    2022-10-09
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作