当前位置:   article > 正文

【Python Web】Flask框架(十)前端+python+MYSQL案例_python+mysql+django/flask

python+mysql+django/flask

前端+python+MYSQL案例

安装插件

pip3 install flask  
pip install cryptography 
pip3 install pymysql 
  • 1
  • 2
  • 3

文件位置

在这里插入图片描述

新增用户

  • app.py
from flask import Flask,render_template, request
import pymysql


app = Flask(__name__)


@app.route('/add/user', methods=["GET","POST"])
def add():
    if request.method == "GET":
        return render_template("add_user.html")

    username = request.form.get("user")
    pwd = request.form.get("pwd")
    mobile = request.form.get("mobile")

    # 连接mysql
    conn = pymysql.connect(host="127.0.0.1", port=3306, user="root", password="root", charset='utf8', db='bkys')
    cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)

    sql = "insert into admin(username,password,mobile) values(%s,%s,%s)"
    cursor.execute(sql, [username, pwd, mobile])
    conn.commit()

    # 关闭
    cursor.close()
    conn.close()

    return "添加成功"

if __name__ == '__main__':
    app.run()

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • add_user.html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>添加用户</title>
</head>
<body>

<h1>添加用户</h1>
<form method="post" action="/add/user">
    <input type="text" name="user" placeholder="用户名">
    <input type="text" name="pwd" placeholder="密码">
    <input type="text" name="mobile" placeholder="手机号">
    <input type="submit" value="提交">
</form>

</body>
</html>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

查询所有用户

  • app.py
from flask import Flask,render_template, request
import pymysql


app = Flask(__name__)


@app.route('/add/user', methods=["GET","POST"])
def add():
    if request.method == "GET":
        return render_template("add_user.html")

    username = request.form.get("user")
    pwd = request.form.get("pwd")
    mobile = request.form.get("mobile")

    # 连接mysql
    conn = pymysql.connect(host="127.0.0.1", port=3306, user="root", password="root", charset='utf8', db='bkys')
    cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)

    sql = "insert into admin(username,password,mobile) values(%s,%s,%s)"
    cursor.execute(sql, [username, pwd, mobile])
    conn.commit()

    # 关闭
    cursor.close()
    conn.close()

    return "添加成功"

@app.route("/show/user")
def show_user():
    # 连接mysql
    conn = pymysql.connect(host="127.0.0.1", port=3306, user="root", password="root", charset='utf8', db='bkys')
    cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)

    sql = "select * from admin"
    cursor.execute(sql)
    data_list = cursor.fetchall()

    # 关闭
    cursor.close()
    conn.close()

    print(data_list)

    return render_template("show_user.html",data_list=data_list)


if __name__ == '__main__':
    app.run()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • show_user.html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>用户列表</title>
</head>
<body>

    <h1>用户列表</h1>
    <table border="1">
        <thead>
            <tr>id</tr>
            <tr>name</tr>
            <tr>user</tr>
            <tr>mobile</tr>
        </thead>
        <tbody>
        {% for item in data_list %}
            <tr>
                <td>{{ item.id }}</td>
                <td>{{ item.username }}</td>
                <td>{{ item.password }}</td>
                <td>{{ item.mobile }}</td>
            </tr>
        {% endfor %}
        </tbody>
    </table>


</body>
</html>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/185722
推荐阅读
相关标签
  

闽ICP备14008679号