iis服务器助手广告广告
返回顶部
首页 > 资讯 > 数据库 >3.Python-用Python实现MySQL数据库的增删改查
  • 758
分享到

3.Python-用Python实现MySQL数据库的增删改查

mysql数据库pythonflaskjavascript开发语言青少年编程 2023-10-18 19:10:38 758人浏览 八月长安
摘要

题记         用python实现Mysql数据库的增删改查,以下是具体的代码和操作步骤 安装flask模块         pip install flask 安装mysql.connector模块         pip

题记

        用python实现Mysql数据库的增删改查,以下是具体的代码和操作步骤

安装flask模块

        pip install flask

安装mysql.connector模块

        pip install mysql-connector-Python

编写app.py文件 

        app.py文件如下: 

为什么显示不完整???# JSONify是Flask提供的用于生成jsON响应的函数# mysql.connector是一个用于连接和操作MySQL数据库的Python库from flask import Flask, request, render_template, jsonifyimport mysql.connectorapp = Flask(__name__)# 连接到MySQL数据库# 填写host,用户名,密码,数据库名db = mysql.connector.connect(    host="localhost",    user="root",    passWord="123456",    database="test")# 创建游标对象cursor = db.cursor()# 创建表格(如果不存在)cursor.execute("CREATE TABLE IF NOT EXISTS students (id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(255), age INT)")@app.route('/')def index():    # 查询数据库中的所有数据,cursor是用来执行SQL语句的游标对象    # cursor.fetchall()方法用于获取查询结果的所有行数据,并将其存储在students变量中    # render_template函数用来渲染index.html模板文件    # 在index.html中,通过在模板中使用{% for student in students %}和{% endfor %}来遍历students列表    cursor.execute("SELECT * FROM students")    students = cursor.fetchall()    return render_template('index.html', students=students)@app.route('/add', methods=['POST'])def add():    # request.fORM属性可以获取表单数据    name = request.form['name']    age = request.form['age']    # 向数据库插入数据    # %s是占位符,用于表示后面的值将会被替换    # values变量是一个元组,包含了要插入的name和age的值    # 使用db.commit()方法提交事务,将数据真正地插入到数据库中    sql = "INSERT INTO students (name, age) VALUES (%s, %s)"    values = (name, age)    cursor.execute(sql, values)    db.commit()    return "数据已成功添加到数据库!"# 用于指定id@app.route('/delete/', methods=['GET'])def delete(id):    # 删除指定ID的数据    sql = "DELETE FROM students WHERE id = %s"    values = (id,)    cursor.execute(sql, values)    db.commit()    return "数据已成功删除!"@app.route('/update/', methods=['GET'])def update1(id):    sql = "SELECT * FROM students WHERE id = %s"    values = (id,)    cursor.execute(sql, values)    # 使用cursor.fetchone()方法获取查询结果的第一行数据,并将其存储在student变量中    student = cursor.fetchone()    return render_template('update.html',student=student)@app.route('/update/', methods=['POST'])def update(id):    name = request.form['name']    age = request.form['age']    # 更新指定ID的数据    sql = "UPDATE students SET name = %s, age = %s WHERE id = %s"    values = (name, age, id)    cursor.execute(sql, values)    db.commit()    return "数据已成功更新!"@app.route('/select/', methods=['GET'])def select(id):    # 查询指定ID的数据    sql = "SELECT * FROM students WHERE id = %s"    values = (id,)    cursor.execute(sql, values)    student = cursor.fetchone()    # jsonify()函数将student转换为JSON格式    if student:        return jsonify(student)    else:        return "未找到匹配的数据!"if __name__ == '__main__':    app.run()
# jsonify是Flask提供的用于生成JSON响应的函数# mysql.connector是一个用于连接和操作MySQL数据库的Python库from flask import Flask, request, render_template, jsonifyimport mysql.connectorapp = Flask(__name__)# 连接到MySQL数据库# 填写host,用户名,密码,数据库名db = mysql.connector.connect(    host="localhost",    user="root",    password="123456",    database="test")# 创建游标对象cursor = db.cursor()# 创建表格(如果不存在)cursor.execute("CREATE TABLE IF NOT EXISTS students (id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(255), age INT)")@app.route('/')def index():    # 查询数据库中的所有数据,cursor是用来执行SQL语句的游标对象    # cursor.fetchall()方法用于获取查询结果的所有行数据,并将其存储在students变量中    # render_template函数用来渲染index.html模板文件    # 在index.html中,通过在模板中使用{% for student in students %}和{% endfor %}来遍历students列表    cursor.execute("SELECT * FROM students")    students = cursor.fetchall()    return render_template('index.html', students=students)@app.route('/add', methods=['POST'])def add():    # request.form属性可以获取表单数据    name = request.form['name']    age = request.form['age']    # 向数据库插入数据    # %s是占位符,用于表示后面的值将会被替换    # values变量是一个元组,包含了要插入的name和age的值    # 使用db.commit()方法提交事务,将数据真正地插入到数据库中    sql = "INSERT INTO students (name, age) VALUES (%s, %s)"    values = (name, age)    cursor.execute(sql, values)    db.commit()    return "数据已成功添加到数据库!"# 用于指定id@app.route('/delete/', methods=['GET'])def delete(id):    # 删除指定ID的数据    sql = "DELETE FROM students WHERE id = %s"    values = (id,)    cursor.execute(sql, values)    db.commit()    return "数据已成功删除!"@app.route('/update/', methods=['GET'])def update1(id):    sql = "SELECT * FROM students WHERE id = %s"    values = (id,)    cursor.execute(sql, values)    # 使用cursor.fetchone()方法获取查询结果的第一行数据,并将其存储在student变量中    student = cursor.fetchone()    return render_template('update.html',student=student)@app.route('/update/', methods=['POST'])def update(id):    name = request.form['name']    age = request.form['age']    # 更新指定ID的数据    sql = "UPDATE students SET name = %s, age = %s WHERE id = %s"    values = (name, age, id)    cursor.execute(sql, values)    db.commit()    return "数据已成功更新!"@app.route('/select/', methods=['GET'])def select(id):    # 查询指定ID的数据    sql = "SELECT * FROM students WHERE id = %s"    values = (id,)    cursor.execute(sql, values)    student = cursor.fetchone()    # jsonify()函数将student转换为JSON格式    if student:        return jsonify(student)    else:        return "未找到匹配的数据!"if __name__ == '__main__':    app.run()

编写index.html文件 

        index.html文件要放在templates文件夹下

        index.html文件如下: 

    学生管理    

学生管理

{% for student in students %} {% endfor %}
ID 姓名 年龄 操作
{{ student[0] }} {{ student[1] }} {{ student[2] }} https://blog.csdn.net/delete/{{ student[0] }}">删除 Https://blog.csdn.net/update/{{ student[0] }}">修改 搜索

新增学生





    学生管理    

学生管理

{% for student in students %} {% endfor %}
ID 姓名 年龄 操作
{{ student[0] }} {{ student[1] }} {{ student[2] }} 删除 修改 搜索

新增学生





 

 编写update.html文件

        update文件如下: 

    修改学生    

修改学生

{ student[0] }}是一个占位符,将会被具体的学生ID替换-->
ion="https://blog.csdn.net/update/{{ student[0] }}" method="POST">



    修改学生    

修改学生

{ student[0] }}是一个占位符,将会被具体的学生ID替换-->




 

执行程序

        启动命令:

        python app.py 

        访问地址:

        localhost:5000 

展示图 

 觉得有用可以收藏或点赞!

来源地址:https://blog.csdn.net/m0_70819559/article/details/133818876

您可能感兴趣的文档:

--结束END--

本文标题: 3.Python-用Python实现MySQL数据库的增删改查

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

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

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

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

下载Word文档
猜你喜欢
  • oracle怎么查询当前用户所有的表
    要查询当前用户拥有的所有表,可以使用以下 sql 命令:select * from user_tables; 如何查询当前用户拥有的所有表 要查询当前用户拥有的所有表,可以使...
    99+
    2024-05-15
    oracle
  • oracle怎么备份表中数据
    oracle 表数据备份的方法包括:导出数据 (exp):将表数据导出到外部文件。导入数据 (imp):将导出文件中的数据导入表中。用户管理的备份 (umr):允许用户控制备份和恢复过程...
    99+
    2024-05-15
    oracle
  • oracle怎么做到数据实时备份
    oracle 实时备份通过持续保持数据库和事务日志的副本来实现数据保护,提供快速恢复。实现机制主要包括归档重做日志和 asm 卷管理系统。它最小化数据丢失、加快恢复时间、消除手动备份任务...
    99+
    2024-05-15
    oracle 数据丢失
  • oracle怎么查询所有的表空间
    要查询 oracle 中的所有表空间,可以使用 sql 语句 "select tablespace_name from dba_tablespaces",其中 dba_tabl...
    99+
    2024-05-15
    oracle
  • oracle怎么创建新用户并赋予权限设置
    答案:要创建 oracle 新用户,请执行以下步骤:以具有 create user 权限的用户身份登录;在 sql*plus 窗口中输入 create user identified ...
    99+
    2024-05-15
    oracle
  • oracle怎么建立新用户
    在 oracle 数据库中创建用户的方法:使用 sql*plus 连接数据库;使用 create user 语法创建新用户;根据用户需要授予权限;注销并重新登录以使更改生效。 如何在 ...
    99+
    2024-05-15
    oracle
  • oracle怎么创建新用户并赋予权限密码
    本教程详细介绍了如何使用 oracle 创建一个新用户并授予其权限:创建新用户并设置密码。授予对特定表的读写权限。授予创建序列的权限。根据需要授予其他权限。 如何使用 Oracle 创...
    99+
    2024-05-15
    oracle
  • oracle怎么查询时间段内的数据记录表
    在 oracle 数据库中查询指定时间段内的数据记录表,可以使用 between 操作符,用于比较日期或时间的范围。语法:select * from table_name wh...
    99+
    2024-05-15
    oracle
  • oracle怎么查看表的分区
    问题:如何查看 oracle 表的分区?步骤:查询数据字典视图 all_tab_partitions,指定表名。结果显示分区名称、上边界值和下边界值。 如何查看 Oracle 表的分区...
    99+
    2024-05-15
    oracle
  • oracle怎么导入dump文件
    要导入 dump 文件,请先停止 oracle 服务,然后使用 impdp 命令。步骤包括:停止 oracle 数据库服务。导航到 oracle 数据泵工具目录。使用 impdp 命令导...
    99+
    2024-05-15
    oracle
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作