SQLite总结

数据库:存储数据的仓库-永久性存储/持久性存储
Sqlite数据库:C开发,开源,嵌入式的数据库
也是当前移动端开发使用最为广泛的数据库
优点:性能良好、零成本管理
支持市场上常见的操作系统
不区分大小写,没有数据类型
但是,我们一般在使用过程中,会指名字段的数据类型




SQL:结构化查询语言
主要是操作数据库的语言,用于数据库的通信


Sqlite3软件命令:
操作sqlite3.exe软件的命令
以.开头,没有分号
.help:显示软件命令
.open:打开数据库文件,如果文件不存在会创建对应的文件,数据库文件以db为后缀名
.tables:查询当前数据库中的所有表名
.schema:查看所有的建表语句
.output:设置显示结果的方向--一般用于数据的导出,.output stdout:重新将结果显示在软件中
.import 文件名 表名:将指定的文件中的数据添加到指定的表中




SQL语句:
使用SQL语言组成的语句,用来操作数据库
直接写语句,以分号结尾


DDL:操作数据库定义语句
主键:表中唯一,不能为null




对表的操作
create table if not exists Student(_id INTEGER primary key autoincrement,name varchar(20) not null,sex varchar(2))
1. 创建表语句  create table Student11(no INTEGER primary key,name varchar(10) not null);
2. 修改表的语句  在表中加入age字段 只能加字段不能减字段 alter table Student add age INTEGER;
3. 删除表语句  drop table Student;


对数据的操作
1. 添加数据语句 insert into Student(no,name,age) values(16070001,"张三",16);
2. 修改数据语句 update Student set sex="男";       
                修改Student表学号为16070002的学生的性别
update Student set sex="女" where no=16070002;
3. 删除数据语句 
    删除学生表中学号为16070003的学生   delete from Student where no=16070003;
假删 加一个字段,是否有效数据 0 1;

4. 查询语句
查询Student表中的所有学生的name和sex
select name,sex from Student;
查询Student表中所有数据
select * from Student;
查询年龄大于25岁的学生
select * from Student where age>25;


查询年龄大于20小于25的学生
select * from Student where age between 21 and 24;
select * from Student where age>20 and age<25;
查询Student年龄不等于20的学生
select * from Student where age!=20;
select * from Student where age<>20;
select * from Student where age<20 or age>20;
查询年龄为16、18、30 的学生
select * from Student where age in(16,18,30);
insert into student (no,name,sex,age) values(129,"小b","男",25);
select * from student;




查询姓名以张的开头的学生--sqlite不支持中文的模糊查询
select * from Student where name like "张%";
查询年龄以2开头的学生
select * from Student where age like "2%";
查询年龄以0结尾的学生
select * from Student where age like "%0";
查询年龄包含2的学生
select * from Student where age like "%2%";
查询年龄以1开头并且1后面有2个字符的学生
select * from Student where age like "1__";
查询年龄不是以1开头的学生
select * from Student where age not like "1%";
分页查询
从第一行开始查询,查询3条数据
select * from Student  limit 0,3;
从第五行开始查询,查询6条数据,如果不够6条就显示剩余的数据
select * from Student  limit 4,6;


查询Student表所有学生 按照年龄进行升序排列
select * from Student order by age;
select * from Student order by age ASC;//升序
select * from Student order by age DESC;//降序


查询当前Student表中的数据的个数
select COUNT(no) from Student;
查询Student中年龄的平均值
select AVG(age) from Student;
查询Student中最大的年龄和最小的年龄
select MAX(age),MIN(age) from Student;
查询Student中年龄的总和
select SUM(age) from Student;


查询Student表中每种性别的人数
select sex,COUNT(no) as CT from Student group by sex;


update Student set age=110 where age >=30;


insert into Student values(16070006,"ccc","男");
insert into Student values(16070005,"bbb","女");
insert into Student values(16070008,"李七","女");
insert into Student values(16070010,"李八",10,"男");
insert into Student values(16070012,"李久",30,"男");
insert into Student values(16070022,"李是",36,"男");
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值