前景提要:文中多次出现的D:\software\A-MySQL\mysql-5.7.31-winx64\bin\这个路径其实应该是D:\software\A-MySQL\mysql-8.4.0-winx64\mysql-8.4.0-winx64\bin\,因为我一开始是MySQL5.7.31版本,后来使用django4用不了mysql5这个老版本,故重新换成了mysql8.4.0这个新版本,路径自己懒得重新改了,在此声明。
1.初识网站
(1)默认编写的静态的效果。
(2)动态:需要用到Web框架的功能。
2.动态网站梳理:
(1)首先我们写了一个Flask网站。
(2)我们用户的浏览器则首先找到Flask网站中的url,我们在url定义了一个函数def index()。
(3)index()函数里面我们会接受用户的请求,然后会在我们的这个HTML模板里面把HTML模版给取回来。
(4)而且还可以再取一些数据。我们专门在某个外面去存储数据,Flask可以专门理解为去读取这些数据(文件或者Excel),读到数据之后,数据和HTML都有了,然后进行替换,生成替换完的给用户浏览器返还回去,则用户就可以看到页面了,此时如果储存数据的地数据发生变化(删了一条数据或者添加了几条数据),则用户浏览器再次发起请求的时候,你再去获取数据则是最新的数据。再跟你的页面配合起来,再次看到的数据就和之前用户浏览器看到的数据不一样了,那么就实现动态效果了。
3.数据存储
对于目前的我们来看,都什么可以做到数据的存储:
(1)txt文件
(2)excel文件
对于这两种存储的方法,操作起来会比较麻烦,因为我们在查询我们要的数据的时候我们自己是通过从第一行开始一行一行的读取这些数据。我们在修改这些文件的时候,则要把文件重新读一遍然后再重新写回去,这样效率也不高。查询和修改都很费劲(并发能力很差)。
(3)专业的软件:数据库管理系统(MySQL/Oracle/SQLServer/DB2/Access.......)。内部会自动帮你做文件的存储,自动帮你解决比较繁琐的问题,自动帮你解决并发的问题。
(4)MySQL:我们可以把MySQL分为两部分。前一部分用于解析指令(比如对发送创建文件夹的指令进行解析),后一部分用于做文件和数据的存储(将数据存储到文件夹下面的文件中)。
因此对文件和文件夹的操作我们不用关注,我们只需关注这些操作的指令就可以了。
4.安装
(1)安装Windows的补丁vcredist_x64.exe和dxwebsetup.exe。
(2)安装MySQL。
(3)创建配置文件my.ini(写在mysql的安装目录下)。
my.ini用记事本打开写入以下代码:
端口号我们设为3306。
[mysqld]
# port
port=3306
# 此处所有的路径要求是你MySQL的安装路径。
# set basedir to your installation path
basedir = D:\\software\\A-MySQL\\mysql-8.4.0-winx64\\mysql-8.4.0-winx64
# set datadir to the location of your data directory
datadir = D:\\software\\A-MySQL\\mysql-8.4.0-winx64\\mysql-8.4.0-winx64\\data
datadir:以后MySQL帮助我们创建文件和文件夹的时候就会默认创建在当前的data目录下。
5.初始化
(1)打开终端(cmd)并且以管理员的身份去运行。
(2)在终端(cmd)中输入初始化的命令。(注意:这里都是mysqld.exe)
D:\software\A-MySQL\mysql-8.4.0-winx64\mysql-8.4.0-winx64\bin\mysqld.exe --initialize-insecure
运行之后mysql的安装目录中会出现一个data的文件夹。
(3)至此,Mysql安装已经完成。
6.启动MySQL
启动
MySQL一般有两种方式:(注意:这里都是mysqld.exe)
(1)临时启动(不建议)
在终端(cmd)中输入(注意:此时的终端都需要以管理员的身份运行):
>>> "D:\software\A-MySQL\mysql-5.7.31-winx64\bin\mysqld.exe"
这种不好,每次启动MySQL太麻烦黑框一旦关掉MySQL就关闭了。
(2)制作成Windows服务,以后通过这个服务来进行关闭和开启。(注意:这里都是mysqld.exe)
<1>制作服务(制作服务打开终端(cmd)时必须以管理员身份运行):
# mysql57这个名字可以随便取
"D:\software\A-MySQL\mysql-5.7.31-winx64\bin\mysqld.exe" --install mysql57
服务制作成功(注意:此时的终端都需要管理员身份运行):
<2>启动服务和关闭服务(注意这里依旧是要以管理员身份运行终端)
1.在命令行中操作:
>>> net start mysql57 # 启动服务
>>> net stop mysql57 # 关闭服务
2.在任务管理器中操作:
3.连接测试
用mysql自带的工具(mysql.exe)连接mysql数据库:
# mysql.exe是mysql自带的连接工具,127.0.0.1是本机的IP地址,你想连那台服务器你就选择用哪台的IP地址
3306是端口号(使我们前面自己设置的),-u root指定使用root用户进行连接,-p 通常是数据库的root用户的密码。
>>> "D:\software\A-MySQL\mysql-5.7.31-winx64\bin\mysql.exe" -h 127.0.0.1 -P 3306
-u root -p
精简(默认连接的是自己的电脑,因此-h和P都可以省略):
>>> "D:\software\A-MySQL\mysql-5.7.31-winx64\bin\mysql.exe" -u root -p
如果你将D:\software\A-MySQL\mysql-5.7.31-winx64\bin\ 添加到系统的环境变量,那么将不用每次输入mysql的路径从而更加的简便。前面的一大串路径就不用写了,代码如下:
>>> mysql -u root -p
mysql环境变量的配置:
在path中添加D:\software\A-MySQL\mysql-8.4.0-winx64\mysql-8.4.0-winx64\bin这个目录:
终端(cmd)登录成功(此时的终端不需要管理员运行):
7.设置密码
注意一定是上面那个先写成功后登录进了mysql也就是出现mysql>这个前缀
set password = password('201967yc');
密码设置成功:
注意:上面的只能用与mysql5 ,如果你的mysql是8版本则不适用。
mysql8你需要按一下的操作在终端中进行操作:
# 显示所有数据库
show databases;
# 选中mysql这个数据库
use mysql;
# 展示所有表
show tables;
# 查询“user”这个表所有信息
select * from user;
# 查询“user”表中“host”、“user”、“authentication_string”和“plugin”这几列的数据
select host,user,authentication_string,plugin from user;
# 刷新mysql系统权限相关表
flush privileges;
# 修改“user”表中的密码信息
alter user 'root'@'localhost' identified by '201967yc'
8.查看已有的文件夹(数据库)
show databases;
9.退出(关闭连接)
10.再连接时,每次登录都要输入密码。
【总结】:
汇总命令:
(1)进入mysql之前(直接在终端上执行的命令):
mysql -u root -p
(2)进入mysql之后
set password = password('201967yc'); # 设置密码
show databases; # 查看已有文件夹
exit; # 退出(关闭连接)
二.MySQL指令
在MySQL和我们平时认知不同的概念:
MySQL | 认知 |
数据库 | 文件夹 |
数据表 | 文件(excel文件) |
在MySQL中我们叫文件夹不叫文件夹而是叫数据库,我们叫文件不叫文件而是叫数据表。我们称对数据库操作实际上就是对文件夹进行操作,对数据表进行操作实际上就是对文件进行操作。
1.数据库管理(文件夹)
(1)查看已有的数据库(文件夹)
show databases;
(2)创建数据库(文件夹)
create database 数据库名字; # 但是不建议直接这样写,因为一些编码的问题可能导致出现错误
create database 数据量名字 DEFAULT CHARSET utf8 COLLATE utf8_general_ci; # 建议用这个方法创建数据库,设置了数据库的默认字符集为utf8。(utf8支持中文)
创建成功:
(3)删除数据库(文件夹)
drop database 数据库名字;
删除成功:
(4)进入数据库(文件夹)
use 数据库名字;
(5)查看文件夹下所有的数据表(文件)
show tables;
查看数据表成功:
(1)进入sys数据库
(2)查看sys文件夹下所有的数据表
三.数据表的管理(文件)
1.进入数据库(进入文件夹),不进入数据库则不能创建表。也就是说use 数据库必须记得操作,否则无法操作。
use 数据库; # 不进入数据库则不能创建表
2.查看当前数据库下的所有表(文件)
show tables;
3.创建表(文件)
create table 表名称(
列名称 类型,
列名称 类型,
列名称 类型
)default charset=utf8;
我们想通过mysql创建如下表:
创建表格:
create table tb1(id int,name varchar(16),age int)default charset=utf8;
(1)当然你也可以写成:
create table tb1(
id int,
name varchar(16),
age int
)default charset=utf8;
因为在mysql中看到分号才认为是结束,可以一行一行的输入直到分号出现截止。
创建结果如下:
(2)当我们去创建一张表的时候是否某一列允许为空一开始的时候要标记上:
create table tb1(
id int,
name varchar(16) not null, # 不允许为空
age int null # 允许为空(默认)
)default charset=utf8;
(3)还可以支持默认值:
create table tb1(
id int,
name varchar(16),
age int defualt 3 # 插入数据时,age列的值默认为3
)default charset=utf8;
(4)创建表的时候一般会在id这一项加一个主键,这一行数据不能为空也不允许重复(主键一般用于表示当前行的数据的编号,类似于人的身份证)
create table tb1(
id int primary key, # 主键(不允许为空,不允许重复)
name varchar(16),
age int
)default charset=utf8;
(5)自增id
create table tb1(
id int auto_increment primary key, # 内部维护,自增。
name varchar(16),
age int
)default charset=utf8;
一般情况下,我们创建表时都会这样写(让id自增,我们就不用去额外管它):
create table tb1(
id int not null auto_increment primary key, # id不允许为空,id自增且id是主键。
name varchar(16),
age int
)default charset=utf8;
desc 表名称; # 用于查看表的结构(即行和列的数据)
创建表的结果如下:
(6)删除表
drop table 表名称;
四.常用数据类型:
(1)tinyint(默认有符号)
tinyint 有符号,取值范围:-128~127(有正有负) (默认有符号)
tinyint unsigned 无符号,取值范围:0~255(只有正)
create table tb1(
id int not null auto_increment primary key, # id不允许为空,id自增且id是主键。
age tinyint # 默认是有符号:取值范围:-128~127。
)default charset=utf8;
create table tb1(
id int not null auto_increment primary key, # id不允许为空,id自增且id是主键。
age tinyint unsigned # 无符号:取值范围:0~255。
)default charset=utf8;
(2)int
int 表示有符号,取值范围:-2147483648 ~ 2147483647
int unsigned 表示无符号,取值范围:0 ~ 4294967295
(3)bigint
bigint 表示有符号,取值范围:-9223372036854775808 ~ 9223372036854775807
bigint unsigned 表示无符号,取值范围:0 ~ 18446744073709551615
【注意】:放入合适的数据类型中,越大的数据类型占用的空间越大。
练习题:
# 创建表
create table tb2(
id bigint not null auto_increment primary key,
salary int,
age tinyint
)default charset=utf8;
# 插入数据
insert into tb2(salary,age) values(10000,18);
insert into tb2(salary,age) values(20000,28);
insert into tb2(salary,age) values(30000,38),(40000,48);
# 查看表中的数据
select * from tb2;
(4)float
(5)double
(6)decimal(精确的小数值)
decimal(m,d):准确的小数值,m是数字总个数(负号不算),d是小数点后个数。m最大值为65,d最大值为30。
例如:
create table tb3(
id int not null primary key auto_increment,
salary decimal(8,2) # 保留小数点后两位。那么小数点前面的数字个数最大不能超过:8-2=6位。
)default charset=utf8;
insert into tb3(salary) values(1.28);
insert into tb3(salary) values(5.289);
insert into tb3(salary) values(5.282);
(7)char(m)(定长字符串)(优点:速度快)
# char(m):定长字符串。m代表字符串的长度,最多可容纳255个字符。
# char(11):固定会用11个字符串进行存储,哪怕真实的没有11个字符也会存够11个字符。
create table tb4(
id int not null primary key auto_increment,
mobile char(11) # 固定会用11个字符串进行存储。
)default charset=utf8;
insert into tb4(mobile) values("151");
insert into tb4(mobile) values("12345678992");
(8)varchar(变长字符串)(优点:节省空间)
# varchar(m):变长字符串。m代表字符的长度。最大65535字节/3 = 最大的m。
# varchar(11):真实数据有多长,就按多长存储。
create table tb5(
id int not null primary key auto_increment,
mobile varchar(11)
)default charset=utf8;
insert into tb5(mobile) values("151");
insert into tb5(mobile) values("1513333333");
注意:能确定长度进行固定的就用char,不能固定长度的就用varchar。
(9)text
/*
text数据类型用于保存变长的大字符串,可以最多到655352个字符。
一般情况下,长文本会用到text类型。例如:文章,新闻等。
*/
create table tb6(
id int not null primary key auto_increment,
title varchar(128),
content text
)default charse=utf8;
(10)mediumtext
(11)longtext
(12)datetime
YYYY-MM-DD HH:MM:SS(1000-01-01 00:00:00/9999-12-31 23:59:59)
(13)date
YYYY-MM-DD(1000-01-01/9999-12-31)
练习题:
(1)进入cmd界面之后首先:
>>> mysql -u root -p # 登录mysql,输入密码即可。
>>> use mysql # 进入mysql数据库。
(2)创建数据表:
create table tb7(
id int not null primary key auto_increment,
name varchar(64) not null,
password char(64) not null,
email varchar(64) not null,
age tinyint,
salary decimal(10,2),
ctime datetime
)default charset=utf8;
insert into tb7(name,password,email,age,salary,ctime) values("小晨","201967yc","192818073@qq.com","24","30000.23","2024-12-21 12:21:21");
insert into tb7(name,password,email,age,salary,ctime) values("小张","201967yc","192818073@qq.com","24","30000.23","2024-12-21 12:21:21");
insert into tb7(name,password,email,age,salary,ctime) values("小李","201967yc","192818073@qq.com","24","30000.23","2024-12-21 12:21:21");
insert into tb7(name,password,email,age,salary,ctime) values("小王","201967yc","192818073@qq.com","24","30000.23","2024-12-21 12:21:21");
insert into tb7(name,password,email,age,salary,ctime) values("小熊","201967yc","192818073@qq.com","24","30000.23","2024-12-21 12:21:21");
(3)运行结果:
我们平时开发系统时,一般情况下:
·创建数据库
·创建表结构
都是需要提前通过上述命令创建。
五.数据行操作
1.新增数据
insert into 表名(列名,列名) values(值,值)
insert into 表名(列名,列名) values(值,值),(值,值),(值,值),(值,值);
create table tb7(
id int not null primary key auto_increment,
name varchar(64) not null,
password char(64) not null,
email varchar(64) not null,
age tinyint,
salary decimal(10,2),
ctime datetime
)default charset=utf8;
# 新增数据
insert into tb7(name,password,email,age,salary,ctime) values("小晨","201967yc","192818073@qq.com","24","30000.23","2024-12-21 12:21:21");
insert into tb7(name,password,email,age,salary,ctime) values("小张","201967yc","192818073@qq.com","24","30000.23","2024-12-21 12:21:21");
insert into tb7(name,password,email,age,salary,ctime) values("小李","201967yc","192818073@qq.com","24","30000.23","2024-12-21 12:21:21");
insert into tb7(name,password,email,age,salary,ctime) values("小王","201967yc","192818073@qq.com","24","30000.23","2024-12-21 12:21:21");
insert into tb7(name,password,email,age,salary,ctime) values("小熊","201967yc","192818073@qq.com","24","30000.23","2024-12-21 12:21:21");
若此处想要增加数据直接在终端输入:
insert into tb7(name,password,email,age,salary,ctime) values("小熊","201967yc","192818073@qq.com","24","30000.23","2024-12-21 12:21:21");
2.删除数据
delete from 表名;
delete from 表名 where 条件; # 删除符合条件的数据。
delete from tb7;
delete from tb7 where id = 3;
delete from tb7 where id = 3 and name = "小李";
delete from tb7 where id > 4;
delete from tb7 where id >= 4;
delete from tb7 where id != 4;
delete from tb7 where id in (1,5);
3.修改数据
update 表名 set 列=值;
update 表名 set 列=值,列=值;
uodate 表名 set 列=值 where 条件;
update tb7 set password="哈哈哈";
update tb7 set email="哈哈哈" where id > 5;
update tb7 set age=age+10 where id > 5;
4.查询数据
select * from 表名称; # 查询表的所有数据。
select 列名称,列名称 from 表名称;
select 列名称,列名称 from 表名称 where id > 3; # 单独查询需要的列和序列号的数据。
运行结果:
【小结】:
我们平时开发系统时,一般情况下:
·创建数据库
·创建表结构
都是需要提前通过工具 + 命令创建。
但是,表中的数据一般情况下都是通过程序(python代码)来实现增删改查。
六.案例:员工管理
1.使用MySQL内置工具(命令)
(1)创建数据库:unicom
(2)创建一张表:admin
表名:admin
列:
id,整型,自增,主键。
username 字符串 不为空,
password 字符串 不为空,
mobile 字符串 不为空。
2.python代码实现:
(1)添加用户
(2)删除用户
(3)查看用户
(4)更新用户信息
3.创建表结构
<1>创建一个叫unicom的数据库:
create database unicom DEFAULT CHARSET utf8 COLLATE utf8_general_ci;
<2>进入数据库
use unicom;
<3>创建一张叫admin的表:
create table admin(
id int not null primary key auto_increment,
username varchar(64) not null,
password char(64) not null,
mobile char(64) not null
)default charset=utf8;
insert into admin(username,password,mobile) values("小晨","201967yc","13476068651");
insert into admin(username,password,mobile) values("小王","201967yc","13476068651");
insert into admin(username,password,mobile) values("小夏","201967yc","13476068651");
运行结果:
4.Python操作MySQL(以前的指令是在终端上进行执行,现在的指令是在python代码中进行执行)
使用python代码连接到MySQL并发送指令。
>>>pip install pymysql # 下载pymysql包
(1)创建数据库
import pymysql
# 1.连接MySQL
conn = pymysql.connect(host="127.0.0.1", port=3306, user="root", passwd="201967yc", charset="utf8", db="unicom") # 此处的user="root", passwd="201967yc"是你一开始设定好的,db="unicom"是你要创建的数据库。
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor) # 基于cursor去发送指令。
# 2.发送指令
cursor.execute("insert into admin(username,password,mobile) values('杨晨','201967yc','13476068651')") # 由于两边是双引号了内部想表达字符串则要用单引号。
conn.commit() # 提交命令
# 3.关闭
cursor.close()
conn.close()
(2)创建数据库的三种方案
import pymysql
# 1.连接MySQL
conn = pymysql.connect(host="127.0.0.1", port=3306, user="root", passwd="201967yc", charset="utf8", db="unicom") # 此处的user="root", passwd="201967yc"是你一开始设定好的,db="unicom"是你要创建的数据库。
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor) # 基于cursor去发送指令。
# 2.发送指令(千万不要用字符串格式化去做SQL的拼接,安全隐患SQL注入,而是使用execute的第二个参数来对它进行传值)
# 方案一:
# cursor.execute("insert into admin(username,password,mobile) values('杨晨','201967yc','13476068651')") # 由于两边是双引号了内部想表达字符串则要用单引号。
# 方案二:
sql = "insert into admin(username,password,mobile) values(%s,%s,%s)"
cursor.execute(sql,["小夏","201967yc","13476068651"])
# 方案三:
# sql = "insert into admin(username,password,mobile) values(%(n1)s,%(n2)s,%(n3)s)"
# cursor.execute(sql,{"n1":"小张","n2":"201967yc","n3":"13476068651"})
conn.commit() # 提交命令
# 3.关闭
cursor.close()
conn.close()
(3)动态创建数据库
import pymysql
# 动态的创建数据库
while True:
user = input("用户名:")
if user.upper() == "Q":
break
pwd = input("密码:")
mobile = input("手机号:")
# 1.连接MySQL
conn = pymysql.connect(host="127.0.0.1", port=3306, user="root", passwd="201967yc", charset="utf8", db="unicom") # 此处的user="root", passwd="201967yc"是你一开始设定好的,db="unicom"是你要创建的数据库。
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor) # 基于cursor去发送指令。
# 发送指令(千万不要用字符串格式化去做SQL的拼接,安全隐患SQL注入,而是使用execute的第二个参数来对它进行传值)
sql = "insert into admin(username,password,mobile) values(%s,%s,%s)"
cursor.execute(sql,[user, pwd, mobile])
conn.commit() # 提交命令
# 3.关闭
cursor.close()
conn.close()
(4)查询数据
import pymysql
# 1.连接MySQL
conn = pymysql.connect(host="127.0.0.1", port=3306, user="root", passwd="201967yc", charset="utf8",db="unicom") # 此处的user="root", passwd="201967yc"是你一开始设定好的,db="unicom"是你要创建的数据库。
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor) # 基于cursor去发送指令。
# 2.发送指令(千万不要用字符串格式化去做SQL的拼接,安全隐患SQL注入,而是使用execute的第二个参数来对它进行传值)
cursor.execute("select * from admin where id > %s",[2,]) # 同样要用到execute的第二个参数来避免用到字符格式化。
# 查询符合条件的所有数据,得到的是[字典,字典](列表里面套字典)-->没有数据:空列表
data_list = cursor.fetchall() # fetchall():用于获取列表中的符合条件的所有数据。
for row_list in data_list:
print(row_list)
# 查询符合条件的第一条数据,得到的是字典 -->没有数据:None
# res = cursor.fetchone() # fetchone():用于获取列表中符合条件的第一条数据。
# print(res)
# 3.关闭连接
cursor.close()
conn.close()
(5)删除数据
import pymysql
# 1.连接MySQL
conn = pymysql.connect(host="127.0.0.1", port=3306, user="root", passwd="201967yc", charset="utf8",db="unicom") # 此处的user="root", passwd="201967yc"是你一开始设定好的,db="unicom"是你要创建的数据库。
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor) # 基于cursor去发送指令。
# 2.发送指令(千万不要用字符串格式化去做SQL的拼接,安全隐患SQL注入,而是使用execute的第二个参数来对它进行传值)
cursor.execute("delete from admin where id=%s",[3,]) # 同样要用到execute的第二个参数来避免用到字符格式化。
conn.commit()
# 3.关闭连接
cursor.close()
conn.close()
(6)修改语句
import pymysql
# 1.连接MySQL
conn = pymysql.connect(host="127.0.0.1", port=3306, user="root", passwd="201967yc", charset="utf8",db="unicom") # 此处的user="root", passwd="201967yc"是你一开始设定好的,db="unicom"是你要创建的数据库。
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor) # 基于cursor去发送指令。
# 2.发送指令(千万不要用字符串格式化去做SQL的拼接,安全隐患SQL注入,而是使用execute的第二个参数来对它进行传值)
cursor.execute("update admin set mobile=%s where id=%s",["88888888",4,]) # 同样要用到execute的第二个参数来避免用到字符格式化。
conn.commit()
# 3.关闭连接
cursor.close()
conn.close()
【小结】:
(1)在进行新增,删除,修改时,一定要记得conn.commit(),不然数据库没有数据。
cursor.execute("...")
conn.commit()
(2)在查询时,不需要commit,但是要执行fetchall/fetchone
cursor.execute("...")
# 查询符合条件的所有数据,得到的是[字典,字典](列表里面套字典)-->没有数据:空列表
v1 = cursor.fetchall()
# 查询符合条件的第一条数据,得到的是字典 -->没有数据:None
v1 = cursor.fetchone()
(3)对于SQL语句不要用python的字符串格式化进行拼接(会被SQL注入),一定要用execute的
第二个参数来对它进行传值。
cursor.execute("...%s",["xx","xx"])
七.案例:Flask+MySQL
(1)新增用户
(2)查询数据
代码实现:
<1>app.py:
from flask import Flask, render_template, request
import pymysql
app = Flask(__name__)
# 新增数据
@app.route("/add/user",methods=["GET","POST"])
def add_user():
if request.method == "GET":
return render_template("add_user.html")
username = request.form.get("user")
password = request.form.get("pwd")
mobile = request.form.get("mobile")
# 1.连接MySQL
conn = pymysql.connect(host="127.0.0.1", port=3306, user="root", passwd="201967yc", charset="utf8", db="unicom") # 此处的user="root", passwd="201967yc"是你一开始设定好的,db="unicom"是你要创建的数据库。
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor) # 基于cursor去发送指令。
# 2.发送指令
sql = "insert into admin(username,password,mobile) values(%s, %s, %s)"
# cursor.execute("insert into admin(username,password,mobile) values('杨晨','201967yc','13476068651')") # 由于两边是双引号了内部想表达字符串则要用单引号。
cursor.execute(sql, [username, password, mobile]) # 用户提交的username,password,mobile放入列表中并执行。
conn.commit() # 提交命令
# 3.关闭连接
cursor.close()
conn.close()
return "添加成功"
# 查询数据
@app.route("/show/user")
def show_user():
# 1.连接MySQL
conn = pymysql.connect(host="127.0.0.1", port=3306, user="root", passwd="201967yc", charset="utf8",db="unicom") # 此处的user="root", passwd="201967yc"是你一开始设定好的,db="unicom"是你要创建的数据库。
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor) # 基于cursor去发送指令。
# 2.发送指令
sql = "select * from admin"
# cursor.execute("insert into admin(username,password,mobile) values('杨晨','201967yc','13476068651')") # 由于两边是双引号了内部想表达字符串则要用单引号。
cursor.execute(sql) # 用户提交的username,password,mobile放入列表中并执行。
data_list = cursor.fetchall() # 提交命令
# 3.关闭连接
cursor.close()
conn.close()
print(data_list)
return render_template('show_user.html',data_list=data_list)
if __name__ == '__main__':
app.run()
<2>add_user.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>添加用户</h1>
<form method="post" action="/add/user"> <!--以post方式提交,提交到/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>
<3>show_user.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>用户列表</h1>
<table border="1">
<thead>
<tr>
<th>ID</th>
<th>用户名</th>
<th>密码</th>
<th>电话</th>
</tr>
</thead>
<!-- 1.首先第一步找到index的HTML文件,读取它所有的内容。 -->
<!-- 2.Flask会找到HTML内容中"特殊的占位符",然后进行数据替换。-->
<!-- 3.替换完成后,将替换完的字符串返回给用户的浏览器。-->
<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>