这是主要架构
wdh/
│
├── app.py
└── templates/
└── index.html
└── home.html
└── add.html
└── update.html
└── static/
└── wdh.jpg
└── wdh.db
from flask import Flask, render_template,request,redirect,url_for
import sqlite3
app = Flask(__name__)
@app.route('/')
def home():
return render_template('home.html')
@app.route('/login', methods=['POST'])
def login():
name = request.form.get('name')
pwd = request.form.get('password')
#连接数据库进行查询
conn = sqlite3.connect('wdh.db')
cursor = conn.cursor()
cursor.execute('select * from user;');
res = cursor.fetchall()
for item in res:
name_sql, password_sql = item
if name_sql == name and password_sql == pwd:
return render_template('index.html')
return render_template('home.html', msg='用户名或者密码错误')
@app.route('/add/user',methods=['GET', 'POST'])
def add():
if request.method == 'GET':
return render_template('add.html')
else:
name = request.form.get('name')
pwd = request.form.get('password')
#连接数据库进行查询
conn = sqlite3.connect('wdh.db')
cursor = conn.cursor()
cursor.execute('insert into user values(?,?)',(name, pwd));
conn.commit()
return redirect(url_for('edit'))
@app.route('/edit/user',methods=['GET', 'POST'])
def edit():
#连接数据库进行查询
conn = sqlite3.connect('wdh.db')
cursor = conn.cursor()
cursor.execute('select * from user');
res = cursor.fetchall()
users = res
return render_template('add.html', users = users)
@app.route('/update/user',methods=['GET', 'POST'])
def update():
if request.method == 'GET':
name = request.args.get('name')
#连接数据库进行查询
conn = sqlite3.connect('wdh.db')
cursor = conn.cursor()
sql = "select * from user where name = '%s';"% name
cursor.execute(sql);
res = cursor.fetchone()
return render_template('update.html', name = res[0] , password = res[1], oldname = res[0])
else:
oldname = request.form.get('oldname')
name = request.form.get('name')
pwd = request.form.get('password')
print(oldname, name , pwd)
#连接数据库进行查询
conn = sqlite3.connect('wdh.db')
cursor = conn.cursor()
sql2 = "update user set name = '%s' , password = '%s' where name = '%s'"%(name, pwd, oldname)
cursor.execute(sql2);
conn.commit()
return redirect(url_for('edit'))
if __name__ == '__main__':
app.run(host='127.0.0.1', port=8888)
数据库操作详见:
https://hlcoding.com/blog/share/content/7602/
创建表
sqlite> sqlite3 wdh.db
sqlite> create table info (name text, age int, score int);
sqlite> showtables;
sqlite> create table user (name text, password text);
sqlite> insert into user('wdh','111111');
sqlite> update user set name='upp' , password='111' where name = '0';
update.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>修改用户</title>
</head>
<body>
<h1>修改用户</h1>
<label style="color:red">{{msg}}</label>
<form action="/update/user" method="post">
<label>原来的姓名:</label>
<input type="text" name="oldname" value="{{name}}" >
<label>姓名:</label>
<input type="text" name="name" value="{{name}}" >
<label>密码:</label>
<input type="text" name="password" value='{{password}}'>
<button type="subumit">点击修改</button>
</form>
</body>
</html>
add.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>编辑用户</title>
</head>
<body>
<h1>创建用户</h1>
<label style="color:red">{{msg}}</label>
<form action="/add/user" method="post">
<label>姓名:</label>
<input type="text" name="name" placeholder="请输入您的大名">
<label>密码:</label>
<input type="password" name="password" placeholder="请输入您的密码,至少6位">
<button type="subumit">点击注册</button>
</form>
<table>
<thead>
<th>姓名</th>
<th>密码</th>
<th>操作</th>
</thead>
<tbody>
{% for item in users %}
<tr>
<td><a href="/update/user?name={{item[0]}}">{{item[0]}}</a></td>
<td>{{item[1]}}</td>
</tr>
{% endfor %}
</tbody>
</table>
</body>
</html>
实操数据库:
import sqlite3 as sql
#创建连接
conn = sql.connect("wdh.db")
#创建 cursor变量
cursor = conn.cursor()
#查询
res = cursor.execute("select * from info;")
#fetchall 所有 fetchone 一个
print(res.fetchall())
#插入数据
data = [['蒂娜',18,88],['罗杰',19,87],['罗得',17,60]]
#cursor.execute("insert into info values(?,?,?) ",(data[0][0],data[0][1],data[0][2]))
#提交,否则查询不到
#conn.commit()
for i in range(3):
cursor.execute("insert into info values(?,?,?) ",(data[i][0],data[i][1],data[i][2]))
#提交,否则查询不到
conn.commit()
增加修改按钮:
<a href="/edit/user">编辑用户</a>
—— 本文来自火龙信奥(义乌睿码科技):义乌青少年信息学奥赛与编程教育平台,专注 CSP-J/S、NOIP、GESP 竞赛培训,线上线下融合教学,助力编程升学。网址:hlcoding.com