python中关于sqlite3数据库更新数据的使用

1.文件结构:

其中html文件放置的文件名必须是templates,图片放置的文件名必须是static:
在这里插入图片描述

2.使用的数据库可视化工具DB Browser for sqlite下载:

http://www.sqlitebrowser.org/

3.使用DB Browser for sqlite新建表users.db:

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

4.主main.py文件:

import os
import sqlite3 as lite
from flask import Flask,jsonify,render_template,request,abort,redirect,url_for

app=Flask(__name__)
mysql_path=os.path.join(os.getcwd(),'users.db')
#自定义错误
@app.errorhandler(404)
def get_Error_Handel(error):
    return render_template('404.html'),404

@app.route('/<string:upd_user>/<string:user>/<string:password>',methods=['POST','GET'])
def update(upd_user,user,password):
    conn=lite.connect(mysql_path)
    with conn:
        cur=conn.cursor()
        sql=f"select count(user) from users where user='{upd_user}'"
        cur.execute(sql)
        count=cur.fetchone()[0]
        if count<=0:
            return jsonify('Update user mistake!'),404
        else:
            sql=f"update users set user='{user}', password='{password}' where user='{upd_user}'"
            cur.execute(sql)
            return render_template('success.html')

@app.route('/update',methods=['POST','GET'])
def update_user():
    if request.method=='POST':
        upd_user=request.form.get('upd_username')
        user=request.form.get('username')
        password=request.form.get('password')
        return redirect(url_for('update',upd_user=upd_user,user=user,password=password))
    if request.method=='GET':
        return render_template('update.html')
if __name__ == '__main__':
    app.run(debug=True)

5.success.html文件:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Success Accept</title>
    <style>
        img {
            width:1500px;
            height:700px;
        }
    </style>
</head>
<body>
<!--这里不能使用绝对路径,只能使用http://127.0.0.1:5000/static/success.png方式或者根路径-->
    <img src="http://127.0.0.1:5000/static/success.png">
</body>
</html>

6.404.html文件:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Failure</title>
    <style>
        img {
            width:1500px;
            height:700px;
        }
    </style>
</head>
<body>
    <img src="./static/404.png">
</body>
</html>

7.update.html文件:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Update</title>
    <style>
        div {
            width:200px;
            height:200px;
            margin:auto;
            border:2px solid #000000;
            font-size:15px;
            font-weight:400;
            margin-top:200px;
        }
        div input:last-child {
            margin-top:10px;
        }
    </style>
</head>
<body>
    <div>
        <form action="http://127.0.0.1:5000/update" method="POST">
            <label>更新账号: </label>
            <input type="text" name="upd_username" value="">
            <br>
            <label>账号: </label>
            <input type="text" name="username" value="">
            <br>
            <label>密码: </label>
            <input type="text" name="password" value="">
            <br>
            <input type="submit" name="submit" value="更新">
        </form>
    </div>
</body>
</html>
  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值