Sqlite

Sqlite:目前移动端开发流行最广泛一门数据库
特点:
1、轻量级,资源占用少
2、零成本

3、性能良好

Sqlite3.exe软件:操作的数据库

软件命令:都是以.开始,结尾没有;
.help 获取支持命令
.open   打开数据库,如果不存在的话就新建
.tables 获取当前数据库所有的表名
.schema 获取当前数据库中所有的建表语句
.output 文件名称.后缀 重定向,将输出结果写入到指定的文件中
.output stdout 重定向,将输出重新显示到窗口中
.import 文件名.后缀名 表名 将指定文件中数据导入到指定的表中

SQL语句

SQL语句执行:没有.开始,但是要有;结尾
主键:表中数据唯一,不可重复 primary key
约束条件:not null:不可为空 default(值):默认值 autoincrement自增(常常应用在主键中)
*:所有字段,顺序就是建表时的字段顺序
1、建表语句
格式:CREATE TABLE 表名(字段名称 [数据类型] [约束条件],字段名称 [数据类型] [约束条件],……);
创建一张学生表
create table Student(no int primary key,name varchar(20) not null,age int default(18));

2、修改表结构语句
新增字段
格式:ALTER TABLE 表名 add 字段名称 [数据类型] [约束条件];

对Student表新增一个字段
alter table Student add sex varchar(2);


3、删除表结构
格式:DROP TABLE 表名;

删除表
drop table Student;


4、新增/添加数据
格式:INSERT INTO 表名[(字段1,字段2,……)] values(值1,值2,……);


添加一名学生:标准形式
insert into Student(no,name,age) values(16110001,"国庆军",19);
insert into Student(age,name,no) values(20,"梁晓飞",16110004);
insert into Student(no,name,age) values(161100010,"丁朋强",23);
insert into Student(age,name,no) values(20,"大哥大",16110012);
insert into Student(no,name,age) values(16110013,"刘光一",19);
insert into Student(age,name,no) values(20,"孟涵",16110022);
insert into Student(no,name,age) values(16110029,"王飞",11);
insert into Student(age,name,no) values(20,"梁晓飞",16110004);
添加一名学生:简写形式,值的顺序和建表时字段顺序一致
insert into Student values(16110002,"王宁",22);


select * from Student;查询学生表中的所有内容


5、修改语句
格式:UPDATE 表名 set 字段名称=值 [where 字段名称=值 ]




修改学号为16110004的姓名为大飞
update Student set name="大飞" where no=16110004;


6、删除语句
格式:DELETE from 表名 [where 字段名称=值]


删除学生表中的学号为16110004
delete from Student where no=16110004;


7、查询语句
格式:SELECT [字段名称,……][*] from 表名 [where 字段名称=值] [order by 字段名称 ASC/DESC] [group by 字段名称];


涉及到的关键字:
where:条件
and:并且,同时满足
or:或者,只要满足其一
>,<:大于,小于
<>,!=:不等于
=:等于
between A and B:在A和B之间
in (值1,值2,……):值为小括号里面的其中之一
like:模糊匹配  常常结合%或_一起使用
%:任意个字符 [0,n]
_:一个字符
like "A%":查询以A开头的
like "%A":查询以A结尾的
like "%A%":查询包含A的
like "A_":查询以A开头的,A后面一个字符
like "_A":查询以A结尾的,A前面一个字符
like "_A_":查询包含A的,A前后分别为一个字符
order by 字段名称 ASC/DESC:按照指定字段进行排序,默认升序(ASC),DESC:降序
limit rowIndex,count:从rowIndex索引开始查询,查count条数据




查询学生表中所有数据
select * from Student;
等价于:
select no,name,age from Student;
查询所有学生的名字
select name from Student;
查询学生表中年龄小于18的学生
select * from Student where age<18;
查询所有学生学号大于16110010并且年龄大于20的学生
select * from Student where no>16110010 and age>20;
查询学生年龄小于22或者学号大于等于16110005的学生
select * from Student where age<22 or no>=16110005;
查询学生姓名为王麻子或者age不为20的学生
select * from Student where name="王麻子" or age!=20;
select * from Student where name="王麻子" or age<>20;
查询年龄在18-22之间的学生
select * from Student where age>=18 and age<=22;
select * from Student where age between 18 and 22;
查询年龄为10,11,23的学生
select * from Student where age=10 or age=11 or age=23;
select * from Student where age in (10,11,23);


Sqlite数据库不支持中文的模糊查找 
查询学生名字以王开头的---不可以这么用
select * from Student where name like "王%";
查询年龄以1开头的学生
select * from Student where age like "1_";
查询学号以1结尾,一共2位字符的学生
select * from Student where no like "_1";
查询学号以1结尾字符的学生
select * from Student where no like "%1";
查询学号为1的学生
select * from Student where no like "1";


查询年龄中包含2的学生
select * from Student where age like "%2%";




按照价格进行升序排列
select * from Phone order by price ASC;
等价于:
select * from Phone order by price;
按照价格进行降序排列
select * from Phone order by price DESC;
查询从行索引为0开始查询3条数据
select * from Phone limit 0,3;
查询从行索引为3开始查询3条数据
select * from Phone limit 3,3;
查询Phone表中一共多少条数据
select COUNT(id) from Phone;
查询Phone表中价格的总和
select SUM(price) from Phone;
查询Phone表中最高、最低、平均价格
select MAX(price),MIN(price),AVG(price) from Phone;
根据类型进行分组并统计每组的个数
select type,Count(id) from Phone group by type;
根据类型进行分组并统计个数
select type,Count(1) from Phone group by type;
根据类型进行分组,并计算每种类型的总价钱
select type,SUM(price) from Phone group by type;


常用函数:
COUNT():计算指定列不为null的数量
MAX():获取指定列的最大值
MIN():获取指定列的最小值
SUM():计算指定列的总和
AVG():计算指定列的平均值
date():获取当前时间


排序
分组
常用函数
反射:就是程序在运行中获取类的属性、方法、构造方法等,
还可以执行类中方法,这一次过程我们就称为反射机制

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值