4. Python 简单操作数据库
(1)基本步骤
from pymysql import * # 导包
# 打开数据库链接,不加端口号链接 创建一个connection类
conn = connect(host="localhost", port=3306, user="root",
password="123456", database="shang", charset='utf8')
# 使用数据库连接对象的cursor()方法创建一个游标对象 cursor
cursor = conn.cursor()
# -------------------------------------------开始操作啦
# 使用游标对象的execute()方法执行SQL语句
cursor.execute("select * from goods;") #想做什么做什么
count = cursor.fetchall() # 得到操作后的数据
for i in count:
print("{:^3}{:^3}{:^10}{:^10}{:^10}".format(i[0], i[1], i[2], i[3], i[4]))
# ------------------------------------------结束(必要操作)
# 关闭游标
cursor.close()
# 关闭数据库链接
conn.close()
(2)修改数据库后数据进去了但是还没有保持,
一定要使用connect对象commit()方法
如果要取消修改,使用connect对象的rollback()方法
MySQL基础语句
1. 关于数据库基本操作
----进入数据库
mysql -u root -p
----显示库列表
show databases;
----新建\删除一个数据库
create database student;
drop database student;
----使用/切换数据库
use student;
2.表格基本操作
(1) ---------------------------------创建和添加
----创建表格
create table if not exists gread1(
id int unsigned primary key not null auto_increment,
name varchar(20),
gender enum("男","女","无"),
scor int unsigned
);
//unsigned 无符号
//primary key 关键序
//not null 不能为空
//auto_increment 自动递增
----显示本数据库的所有table
show tables;
----显示table的所有 字段
desc gread1;
----添加表内容
insert into gread1 values(0, "小明" , "男", 100);
insert into gread1 values(0, "小明" , "男", 100),(1, "小陈" , "男", 100);
----显示表格所有内容
select * from gread1;
select (字段名,字段名) from gender1 where id=1;
----显示表的创建语句
show create table gread1;
(2)---------------------------------修改字段 alter table (table名) (模式);
----添加字段 添加一个brithday字段
alter table gread1 add brithday datetime;
----修改字段的数据类型 modify 不修改名字,只修改数据类型
alter table gread1 modify birthday data;
//将上面的datetime改为date类型
----修改字段的名字 和 数据类型
alter table gread1 change birthday birth data;
//将字段birthday 改为 birth 数据类型为date
----删除字段
alter table gread1 drop birth;
----删除表
drop table gread1;
(3)---------------------------------修改表 update (table名) set (id = 1);
----修改表
update stu set gender = 1; //全部设置
update stu set gender = 1,name = "meimei" where id=1; // id = 1 的地方设置
(4)---------------------------------查询表 select (*) from (table名) where () and ();
----查询语句 where 相当于 if
select name from stu where id = 1;
select name as "姓名" ,gender as "性别" from student.stu ;
select name 姓名,gender 性别 from student.stu;
select name as 姓名 ,num as 号码, gender as 性别 from student.stu ;
select “姓名”,name,num from student.stu; //会出现一列“姓名”字符
select * from stu where name in (“xiaohua”,”xiaoli”);
select distinct gender from stu; // distinct 去重
----条件查询 比较运算符 > < = != >= <= <>
select * from stu where age > 18 ;
select * from * where between 18 and 23;//包括18和23
----条件查询 逻辑运算符 and or not
select * from stu where age > 18 and age < 50;
select * from stu where not age > 18 and gender = 1; //not age >18
select * from stu where not (age > 18 and gender = 1); //not (age > 18 and gender = 1)
----like 模糊查询
获取 某字段中 含有 某字符 的信息(%替代零个一个或多个字符、_替代一个字符)
select * from (table名) where (字段) like ('%--')
select * from stu where num like '%10'
select * from stu where name like '%华';
select * from stu where name like '__'; //两个字的名字
select * from stu where name like '___%'; //三个字及以上的名字
----rlike 正则表达式
select * from stu where name rlink '^周.*'; //以周开头
select * from stu where name rlink '^周.伦&'; //以周开头 以杰结尾
----in () 非连续 范围查询
select * from stu where age in (12,16,18); //年龄为12 16 18
----between .. and .. 连续查询
select * from stu where age between 18 and 50; //年龄在18-50 包括二者
select * from stu where age not between 18 and 50; //年龄不在18-50 包括二者
---- 判空 is null 判断非空 is not null
----保留两位小数 round(price,2)
(5)---------------------------------删除数据 delete from (table名) where ();
delete from stu where id = 1; //把id = 1 那一行删除了
delete from stu; //把 stu 清空
3. MySQL高级操作
(1)排序
----select语句使用 order by 子句将查询数据排序后返回 asc/desc:升序/降序
select * from (table名) order by (字段) asc;
select * from stu where num like '%10%' order by num asc;
select * from stu where num like '%10%' order by num desc,high asc; //先按照num排序高到低 当num相同时按照high低到高排序
(2)分组
----group by 语句根据一列或多列对结构集进行分组
select gender,count(gender) from stu group by gender; //显示分组后 显示个体个数
select gender,group_concat(name) from stu group by gender; //显示分组后 显示组里面的个体
select gender,group_concat(name,' ',id,' ' ,num) from stu group by gender; //显示分组后 显示组里面的个体name id num
----having 对分组进行判断
select gender,group_concat(name) from stu group by gender having avg(age)>20; //显示平均年龄大于20 的性别
(3)查找
----limit 限制显示个数
select * from stu limit 3; //只显示三行
select * from stu limit 2,3; //从第三个开始显示三行
(4)连接查询
----内链接 inner join ... on... 显示都有的
select stu.*,class.name from (table1名) inner join (table2名) on (条件); //显示table1中满足table2的条件联系
select stu.*,class.name from stu.name inner join class on stu.class_id=class.id;
----左链接 left join 显示左边有的
select stu.*,class.name from stu left join class on stu.class_id = class.id; //stu里面的全部显示 class没有内容的为null
----右链接 right join
----自关联 inner join on 查同一个表的内容 省-市-县
select p.name,c.name from areas as p inner join area as c on c.c_id = p.p_id having p.name like '重庆%'; //查找重庆的区县
select p.name from areas as p where pid = (select aid where tittle = '重庆省');
(5)子查询
----select * from stu where height = (select max(height) from stu); //查找身高最高的同学
(6)插入
----一张表的数据插入另一张表的数据
insert into class (name) select class_name from stu group by class_name;
//将stu中班级分组后插入class中
(7) 外关联 添加外键
----将本表中的数据和外表进行关联
alter table (table1名) add foreign key (table1字段) references (table2名)(table2字段);
alter table goods add foreign key (cate_id) references good_cates(id);
//将表goods的cate_id与good_cates的id相关联 ,以后插入的时候自动关联,没能关联就报错(有限制)
---- 创建表的时候直接设计外关联
create table stu(
id int unsigned primary key not null auto_increment;
name varchar(20) not null;
foreign key(class_id) references classes(id); //直接关联
);
没有更多啦
我也是刚学一点点参加比赛,爬虫的数据保存到数据库,加油哦