一.数据的存储方式

1.Plist(NSDictionary NSArray)只能存储数组与字典,但是数组与字典不能有自定义对象;

2.编号设置:也不能存储自定义对象

3.归档与解档:能存储自定义对象,局限:一次性读取与存储操作

4.sqlite3:能存储自定义对象没有局限性.操作方便,可与局部的读取,小轻型、占用资源少。

  • sqlite3创建表的时候需要注意事项:1:t_表明  2:主键  3:real 是浮点类型 4:blob : 二进制数据
  • sqlite3中有DDL语句处理数据的定义与删除create drop  DML语句处理数据的insert update delete DQL语句处理数据的查询
  • 创表格式:create table 表名 (字段名1 字段类型1, 字段名2 字段类型2, …) ;

                        create table if not exists 表名 (字段名1 字段类型1, 字段名2 字段类型2, …)

                        示例:create table t_student (id integer primary key autoincrement, name text, age integer, score real) ;
  •  去掉表格式:drop table 表名 ; 

                        drop table if exists 表名 ;

                        示例:drop table t_student ;

  • 插入表格式: insert into 表名 (字段1, 字段2, …) values (字段1的值, 字段2的值, …) ;

                       示例insert into t_student (name, age) values (‘mj’, 10) ;

                       注意:数据库中的字符串内容应该用单引号 ’ 括住

  • 更改表格式:update 表名 set 字段1 = 字段1的值, 字段2 = 字段2的值, … ;

                          示例:update t_student set name = ‘jack’, age = 20 ;

                          注意:上面的示例会将t_student表中所有记录的name都改为jack,age都改为20

  • 删除表格式:delete from 表名 ;(delete from 表名只会删除表里的数据,drop table 表名整个表都删除)
                           示例:delete from t_student ;
                           注意:上面的示例会将t_student表中所有记录都删掉
 
条件语句的常见格式
where 字段 = 某个值 ;   // 不能用两个 =
where 字段 is 某个值 ;   // is 相当于 =
where 字段 != 某个值 ;
where 字段 is not 某个值 ;   // is not 相当于 !=
where 字段 > 某个值 ;
where 字段1 = 某个值 and 字段2 > 某个值 ;  // and相当于C语言中的 &&
where 字段1 = 某个值 or 字段2 = 某个值 ;  //  or 相当于C语言中的 ||
where 字段 like '%p%' 用于模糊查询:意思是含有p的所有字段都给查询
 
示例
将t_student表中年龄大于10 并且 姓名不等于jack的记录,年龄都改为 5
update t_student set age = 5 where age > 10 and name != ‘jack’ ;

 

删除t_student表中年龄小于等于10 或者 年龄大于30的记录
delete from t_student where age <= 10 or age > 30 ;
 
猜猜下面语句的作用
update t_student set score = age where name = ‘jack’ ;
将t_student表中名字等于jack的记录,score字段的值 都改为 age字段的值
 
 
  • 查询表格式:select 字段1, 字段2, … from 表名 ;
                          select * from 表名;   //  查询所有的字段

 

  • 示例:select name, age from t_student ;

                 select * from t_student ;

                 select * from t_student where age > 10 ;  //  条件查询

 

按照某个字段的值,进行排序搜索
select * from t_student order by 字段 ;
select * from t_student order by age ;
 
默认是按照升序排序(由小到大),也可以变为降序(由大到小)
select * from t_student order by age desc ;  //降序
select * from t_student order by age asc ;   // 升序(默认)
 
也可以用多个字段进行排序
select * from t_student order by age asc, height desc ;
先按照年龄排序(升序),年龄相等就按照身高排序(降序)
 
select * from 表名 limit 数值1, 数值2 ;
 
示例
select * from t_student limit 4, 8 ;
可以理解为:跳过最前面4条语句,然后取8条记录
  • 简单约束
建表时可以给特定的字段设置一些约束条件,常见的约束有
not null :规定字段的值不能为null
unique :规定字段的值必须唯一
default :指定字段的默认值
(建议:尽量给字段设定严格的约束,以保证数据的规范性)
 
示例
create table t_student (id integer, name text not null unique, age integer not null default 1) ;
name字段不能为null,并且唯一
age字段不能为null,并且默认为1
 
  • 外键约束
利用外键约束可以用来建立表与表之间的联系
外键的一般情况是:一张表的某个字段,引用着另一张表的主键字段
 
新建一个外键
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);
t_student表中有一个叫做fk_t_student_class_id_t_class_id的外键
这个外键的作用是用t_student表中的class_id字段引用t_class表的id字段
  • 起别名

格式(字段和表都可以起别名)

select 字段1 别名 , 字段2 别名 , … from 表名 别名 ;
select 字段1 别名, 字段2 as 别名, … from 表名 as 别名 ;
select 别名.字段1, 别名.字段2, … from 表名 别名 ;

 示例

select name myname, age myage from t_student ;

给name起个叫做myname的别名,给age起个叫做myage的别名

 select s.name, s.age from t_student s ;

给t_student表起个别名叫做s,利用s来引用表中的字段
 

create table if not exists NCStudent_tmp (stuNum integer, name text, age integer, address text, primary key(stuNum));
insert into NCStudent_tmp (stuNum) select stuNum from NCStudent;
update NCStudent_tmp set name = (select name from NCStudent where NCStudent_tmp.stuNum = NCStudent.stuNum);
drop TABLE if exists NCStudent;
alter table NCStudent_tmp rename to NCStudent;

 
 
 
 
 
 
 
 
 
 
 
 

转载于:https://www.cnblogs.com/520ljw/p/7215532.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值