SQLite3 数据库
以前学过的存储方式:
1.简单存储
2. 存储少的数据
3. 一次性加载所有的数据
分别对应:
存储大量数据:
轻量型 在嵌入式设备 (手机,ipad)上使用
处理速度比较快
SQLite 数据库存储原理:
数据库语句 SQL 语句:
创建表格:
更新:
全部更新:
删除:
全部删除:
- Plist(NSArray\NSDictionary)
- Preference(偏好设置\NSUserDefaults)
- NSCoding(NSKeyedArchiver\NSkeyedUnarchiver)
存储大量数据:
SQLite3 (嵌入式数据库 )
轻量型 在嵌入式设备 (手机,ipad)上使用
处理速度比较快
SQLite 数据库存储原理:
- 新建一张表(table)
- 添加多个字段(column,列,属性)
- 添加多行记录(row,每行存放多个字段对应的值)
作用: 在数据库中操作数据库中的数据
增删改查 (CRUD)
SQL语句特点:
SQL语句特点:
- 不区分大小写(比如数据库认为user和UsEr是一样的)
- 每条语句都必须以分号 ; 结尾
- 不能使用关键字 来命名表名 以及字段名
SQL语句的种类:
n数据定义语句(DDL:Data Definition Language)
p包括create和drop等操作
p在数据库中创建新表或删除表(create table或 drop table)
n数据操作语句(DML:Data Manipulation Language)
p包括insert、update、delete等操作
p上面的3种操作分别用于添加、修改、删除表中的数据
n
n数据查询语句(DQL:Data Query Language)
p可以用于查询获得表中的数据
p关键字select是DQL(也是所有SQL)用得最多的操作
p其他DQL常用的关键字有where,order by,group by和having
用SQL中的DDL语句:创建、删除 表格
SQLite 中默认四种存储类型:integer (整形)、 real (浮点)、 text(文本字符串)、blob(二进制数据)
但是在TA内部并没有划分类型 比如 声明了integer 类型 也能存储 其他类型
创建表格:
- 格式
-
- create table 表名 (字段名1 字段类型1, 字段名2 字段类型2, …) ;
- create table if not exists 表名 (字段名1 字段类型1, 字段名2 字段类 型2, …) ;
- 示例
-
- create table t_student (id integer, name text, age inetger, score real) ;
为了严谨起见:都加上 if not
创建表格时应该 对表格设置主键
删除:
- 格式
-
- drop table 表名 ;
- drop table if exists 表名 ;
- 示例
-
- drop table t_student ;
对已有的表进行编辑(插入数据、更新数据、删除数据)
插入:
插入:
格式:
insert into 表名 (字段1, 字段2, …) values (字段1的值, 字段2的值, …) ;
示例:
insert into t_student (name, age) values (‘one’, 10) ;
注意: 数据库中字符串应该引用单引号 括起来
更新:
全部更新:
- 格式
-
- update 表名 set 字段1 = 字段1的值, 字段2 = 字段2的值, … ;
- 示例
-
- update t_student set name = ‘jack’, age = 20 ;
- update t_student set name = ‘jack’, age = 20 ;
- 注意
-
- 上面的示例会将t_student表中所有记录的name都改为jack,age都改为20;
全部删除:
- 格式
-
- delete from 表名 ;
- 示例
-
- delete from t_student ;
- 注意
-
- 上面的示例会将t_student表中所有记录都删掉
如果想要单独 更新 或 删除 某些东西 就在DML语句后面加上一些条件
- 条件语句的常见格式
-
- where 字段 = 某个值 ; // 不能用两个 =
- where 字段 is 某个值 ; //is 相当于 =
- where 字段 != 某个值 ;
- where 字段 is not 某个值 ; //is not 相当于 !=
- where 字段 ]]> 某个值 ;
- where 字段1 = 某个值 and 字段2 ]]> 某个值 ; // and相当于C语言中的 &&
- where 字段1 = 某个值 or 字段2 = 某个值 ; // or 相当于C语言中的 | |
示例
将t_student表中年龄大于10 并且 姓名不等于jack的记录,年龄都改为 5
update t_student set age = 5 where age ]]]]]]> 10 and name != ‘jack’ ;删除
t_student表中年龄小于等于10 或者 年龄大于30的记录
elete from t_student where age <= 10 or age ]]]]]]> 30 ;
将t_student表中名字等于haha的记录,height字段的值 都改为 age字段的
update t_student set score = age where name = ‘jack’ ;
DQL语句:
- 格式
-
- select 字段1, 字段2, … from 表名 ;
- select * from 表名; // 查询所有的字段
- 示例
-
- select name, age from t_student ;
- select * from t_student ;
- select * from t_student where age ]]> 10 ; // 条件查询
limit 它能精确控制查询结果的数量
使用limit可以精确地控制查询结果的数量,比如每次只查询10条数据
- 格式
-
- select * from 表名 limit 数值1, 数值2 ;
- 示例
-
- select * from t_student limit 4, 8 ;
- 可以理解为:跳过最前面4条语句,然后取8条记录
主键的声明:
- 在创表的时候用primary key声明一个主键
-
- create table t_student (id integer primary key, name text, age integer) ;
- integer类型的id作为t_student表的主键
- 主键字段
-
- 只要声明为primary key,就说明是一个主键字段
- 主键字段默认就包含了not null 和 unique 两个约束
- 如果想要让主键自动增长(必须是integer类型),应该增加autoincrement
-
- create table t_student (id integer primary key autoincrement, name text, age integer) ;
外键:
- 利用外键约束可以用来建立表与表之间的联系
-
- 外键的一般情况是:一张表的某个字段,引用着另一张表的主键字段
- 新建一个外键
-
- create table t_student (id integer primary key autoincrement, name text, age integer, class_id integer, constraint fk_t_student_class_id_t_class_id foreign key (class_id) references t_class (id) on delete cascade on update cascade) ;
- t_student表中有一个叫做fk_t_student_class_id_t_class_id的外键
- 这个外键的作用是用t_student表中的class_id字段引用t_class表的id字段
随机 :
![]()
或者:
![]()
或者:

浙公网安备 33010602011771号