文章目录
mysql
官网:https://www.mysql.com/
1 mysql使用
C:\Windows\System32> mysql -u root -p
输入密码
mysql> show databases;
展示数据库
退出
mysql> exit;
2 mysql重新设置密码
- 停用MySQL服务
- 修改mysql文件
添加 skip-grant-tables=1
无需密码即可进入
输入命令mysql -u root -p指定 root 用户登录 MySQL,输入后按回车键输入密码。如果没有配置环境变量,请在 MySQL 的 bin 目录下登录操作。
输入use mysql;命令连接权限数据库。
输入命令update mysql.user set authentication_string=password(‘新密码’) where user=‘用户名’ and Host =‘localhost’;设置新密码。
输入 flush privileges; 命令刷新权限。
输入quit;命令退出 MySQL 重新登录,此时密码已经修改为刚才输入的新密码了。
修改回配置文件
3 数据库管理
--创建数据库
create database dbname DEFAULT CHARSET utf8 COLLATE utf8_general_ci;
--删除数据库
drop database dbname;
--进入数据库
use dbname;
--查看数据表
show tables;
4 数据表管理
创建表
create table tbname(
列名 类型,
列名 类型,
列名 类型,
)default charset=utf8;
create table tb1(
id int,
name varchar(16) not null, --不允许为空
age int
)default charset=utf8;
create table tb1(
id int,
name varchar(16),
age int default 3 --默认值为3
)default charset=utf8;
create table tb1(
id int primary key, --主键(不空不重)
name varchar(16),
age int
)default charset=utf8;
一般:
create table tb1(
id int not null auto_increment primary key, --id自增非空
name varchar(16),
age int
)default charset=utf8;
查看表
desc 表名称;
删除表
drop table 表名称;
5 常用数据类型
整数
- tinyint
有符号, 取值范围: -128 ~ 127(有正有负)
无符号, 取值范围: 0 ~ 255(只有正)
create table tb1(
id int not null auto_increment primary key,
age tinyint --有符号, 取值范围: -128 ~ 127
) default charset=utf8;
create table tb1(
id int not null auto_increment primary key,
age tinyint unsigned --无符号, 取值范围: 0 ~ 255
) default charset=utf8;
- int
有符号, 取值范围: -2147483648 ~ 2147483647(有正有负)
无符号, 取值范围: 0 ~ 4294967295(只有正)
- bigint
有符号, 取值范围: -9223372036854775808 ~ 9223372036854775807(有正有负)
无符号, 取值范围: 0 ~ 18446744073709551615(只有正)
小数
-
float
-
double
-
decimal(精准小数值,m最大65,小数位数最大30)
create table tb1(
id int auto_increment primary key, --内部维护,自增
name varchar(16),
salary decimal(8,2) --一共8位(整数位数+小数点位数), 保留小数点后2位
) default charset=utf8;
字符串
- char
定长字符串
默认固定用 11 个字符串进行存储,哪怕字符串个数不足,也按照11个字符存储
最多能存储255个字节的数据
查询效率高
- varchar
变长字符串
默认最长 11 个字符,真实数据多长就按多长存储
最多能存储 65535 个字节的数据,中文可存储 65535/3 个汉字
相对 char 类型, 查询效率低
- text
变长字符串
默认最长 11 个字符,真实数据多长就按多长存储
最多能存储 65535 个字节的数据,中文可存储 65535/3 个汉字
相对 char 类型,查询效率低
-
mediumtext
-
longtext
大字符串
时间
- datetime
YYYY-MM-DD HH:MM:SS (1000-01-01 00:00:00/9999-12-31 23:59:59)
- date
YYYY-MM-DD (1000-01-01/9999-12-31)
案例:用户表
create table user(
id int not null primary key auto_increment,
name varchar(64),
password varchar(64),
age tinyint,
salary decimal(10,2),
ctime datetime
)default charset=utf8;
6 数据管理
插入表行数据
insert into tb2(salary,age) values(50,24),(100,30);
查看表数据
select 列名,列名 from 表名称 where 条件;
--ed
select *from tb2;
select id,name from where name="sxk" and password ="123";
删除数据
delete from 表名;--删除整个表
delete from 表名 where 条件;
--ed
delete from tb where id = 3 and name="sxk";
id >2;
id in (1,5);
修改数据
updata 表名 set 列=“值” where id <3;
--ed
updata tb set age=age+10 where age <3;
案例:员工管理
创建表
使用Mysql内置工具(命令)
创建数据库: uu
创建数据表: aa
列:
id 整型 自增 主键
username: 字符串 不为空
password: 字符串 不为空
mobile: 字符串 不为空
mysql> create table aa(
-> id int primary key auto_increment,
-> username char(30) not null,
-> password char(30) not null,
-> mobile char(30) not null)default charset=utf8
-> ;
python创建数据
创建数据
import pymysql
# 链接mysql
conn = pymysql.connect(host='127.0.0.1', port=3306,user='root' , password='root',charset='utf8',db="uu")
cursor =conn.cursor()
# 发送指令
cursor.execute("insert into aa(username,password,mobile) values ('sxk','123','123456')")
conn.commit()
# 关闭
cursor.close()
conn.close()
优化:手动输入
import pymysql
while True:
u = input("用户名")
if u.upper() == 'Q':
break
p = input("密码:")
m = input("手机")
# 链接mysql
conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', password='root', charset='utf8', db="uu")
cursor = conn.cursor()
# 发送指令
sql = "insert into aa(username,password,mobile) values (%s, %s, %s);"
cursor.execute(sql, [u, p, m])
conn.commit()
# 关闭
cursor.close()
conn.close()
python查询数据
# author : Sun; time : 2023/2/3 17:02;
import pymysql
conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', password='root', charset='utf8', db="uu")
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
sql = "select * from aa where id>=3"
cursor.execute(sql)
# data_list = cursor.fetchall() 查询一条数据,为字典
data_list = cursor.fetchall() #查询所有符合条件的数据,为列表套多个
print(data_list )
# 关闭
cursor.close()
conn.close()
输出一个列表套字典:[{字典} ,{字典} …]
[{'id': 3, 'username': 'kk', 'password': '321', 'mobile': '1232121'},
{'id': 4, 'username': 'uu', 'password': '135', 'mobile': '12513535'}]
python删除数据
# author : Sun; time : 2023/2/3 17:02;
import pymysql
conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', password='root', charset='utf8', db="uu")
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
sql = "delete from aa where id =%s"
cursor.execute(sql, [2])
conn.commit()
sql = "select * from aa"
cursor.execute(sql)
data_list = cursor.fetchall()
for i in data_list:
print(i)
# 关闭
cursor.close()
conn.close()
python修改数据
# author : Sun; time : 2023/2/3 17:02;
import pymysql
conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', password='root', charset='utf8', db="uu")
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
sql = "update aa set mobile=%s where id =%s"
cursor.execute(sql, [88888888,1,])
conn.commit()
sql = "select * from aa"
cursor.execute(sql)
data_list = cursor.fetchall()
for i in data_list:
print(i)
# 关闭
cursor.close()
conn.close()
总结
在进行新增、删除、修改时进行,,,不然没有数据
cursor.execute(sql)
conn.commit()
import pymysql
conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', password='root', charset='utf8', db="uu")
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
sql = "sql语句"
cursor.execute(sql)
conn.commit()
# 关闭
cursor.close()
conn.close()
查询时不需要commit
执行fetchall/fachone
import pymysql
conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', password='root', charset='utf8', db="uu")
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
sql = "select * from aa"
cursor.execute(sql)
#所有数据,列表套字典,无数据为None
data_list = cursor.fetchall()
#第一条数据,字典,无数据为空列表
data_list = cursor.fetchone()
# 关闭
cursor.close()
conn.close()
案例:flask + mysql
main.py
from flask import Flask, render_template, request
import pymysql
app = Flask(__name__)
@app.route("/aduser", methods=["POST", "GET"])
def aduser():
if request.method == "GET":
return render_template("xaduser.html")
else:
un = request.form.get("user")
pwd = request.form.get("pwd")
mb = request.form.get("mobile")
conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', password='root', charset='utf8', db="uu")
cursor = conn.cursor()
sql = "insert into aa(username,password,mobile) values (%s,%s,%s);"
cursor.execute(sql, [un, pwd, mb])
conn.commit()
cursor.close()
conn.close()
print(un, pwd, mb)
return "添加成功"
@app.route("/shuser", methods=["POST", "GET"])
def shuer():
conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', password='root', charset='utf8', db="uu")
# 返回类型为元组类型
cursor = conn.cursor()
sql = "select * from aa"
cursor.execute(sql)
data = cursor.fetchall()
cursor.close()
conn.close()
return render_template("xshuser.html", data = data)
if __name__ == '__main__':
app.run()
xaduser.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1> 添加用户</h1>
<form method="post" action="/aduser">
<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>
xshuser.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h2>展示列表</h2>
<div id="app">
<table>
<thead>
<tr>
<th>id</th>
<th>username</th>
<th>pwd</th>
<th>moobile</th>
</tr>
</thead>
<tbody>
{% for d in data %}
<tr>
<th>{{ d[0] }}</th>
<th>{{d[1]}}</th>
<th>{{d[2]}}</th>
<th>{{d[3]}}</th>
</tr>
{% endfor %}
</table>
</div>
</body>
</html>
占位符
- 找到xshuser.html的文件,读取所有内容
- 找到特殊占位符,将数据替换
- 将替换完成的字符串返回给用户
...
sql = "select * from aa"
cursor.execute(sql)
data = cursor.fetchall()
...
return render_template("xshuser.html", data = data)
data为元组类型.
{% for d in data %}
<tr>
<th>{{ d[0] }}</th>
<th>{{d[1]}}</th>
<th>{{d[2]}}</th>
<th>{{d[3]}}</th>
</tr>
{% endfor %}
加入bootstrap
<link rel="stylesheet" href="../static/plugins/bootstrap-3.4.1/css/bootstrap.css"
....
<table class="table table-bordered">