1>存储小数据,会在原路径覆盖式存储mysql
Plist (只能存储NSArray\NSDictionary)
Preference(偏好设置\NSUserDefaults standardUserDefaults)git
NSCoding (NSKeyedArchiver\NSkeyedUnarchiver)github
2>存储大批量数据sql
SQLite3 纯C语言,轻量级,检索速度快
Core Data OC语言数据库
SQLite是一款轻型的嵌入式数据库
它占用资源很是的低,在嵌入式设备中,可能只须要几百K的内存就够了
它的处理速度比Mysql、PostgreSQL这两款著名的数据库都还快api
SQLite将数据划分为如下几种存储类型:
integer : 整型值
real : 浮点值
text : 文本字符串
blob : 二进制数据(好比文件)
实际上SQLite是无类型的
就算声明为integer类型,仍是能存储字符串文本(主键除外)
建表时声明啥类型或者不声明类型均可以,也就意味着创表语句能够这么写:
create table t_student(name, age);安全
1)建立表多线程
create table if not exists 表名 (字段名1 字段类型1, 字段名2 字段类型2, …) ;框架
eg:create table t_student (id integer, name text, age inetger, score real) ;异步
2)删除表
drop table if exists 表名 ;
eg:drop table t_student ;
1)添加数据
insert into 表名 (字段1, 字段2, …) values (字段1的值, 字段2的值, …) ;
eg:insert into t_student (name, age) values (‘mj’, 10) ; //数据库中的字符串内容应该用单引号 ’ 括住
2)删除数据
delete from 表名 ;
eg:delete from t_student ;//上面的示例会将t_student表中全部记录都删掉
3)更新数据
update 表名 set 字段1 = 字段1的值, 字段2 = 字段2的值, … ;
eg:update t_student set name = ‘jack’, age = 20 ; //会将表中全部记录的name都改成jack,age都改成20
4)查找数据
select 字段1, 字段2 (或者*) from 表名 where 字段名 = 值
eg:select name, age from t_student ;
select * from 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语言中的 ||
别名再表链接查询很是有用
1)格式(字段和表均可以起别名)
select 字段1 别名 , 字段2 别名 , … from 表名 别名 ;
select 字段1 别名, 字段2 as 别名, … from 表名 as 别名 ;(别名能够直接写在字段后面,省略as)
select 别名.字段1, 别名.字段2, … from 表名 别名 ;
eg:select name myname, age myage from t_student ;
select s.name, s.age from t_student s ; 给t_student表起个别名叫作s,利用s来引用表中的字段
select count (字段) from 表名 ;
select count ( * ) from 表名 ;
eg:select count (age) from t_student ;
select count ( * ) from t_student where score >= 60;
默认是升序
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 ;
先按照年龄排序(升序),年龄相等就按照身高排序(降序)
1>简单约束
not null :规定字段的值不能为null
unique :规定字段的值必须惟一
default :指定字段的默认值
eg:create table t_student (id integer, name text not null unique, age integer not null default 1) ;
2>主键约束
主键(Primary Key,简称PK)用来惟一地标识某一条记录
例如t_student能够增长一个id字段做为主键,至关于人的身份证
主键能够是一个字段或多个字段,也就是说一张表能够有多个主键,但最好只有1个
1)设计原则
主键应当是对用户没有意义的
永远也不要更新主键
主键不该包含动态变化的数据
主键应当由计算机自动生成
若是想要让主键自动增加(必须是integer类型),应该增长autoincrement
create table t_student (id integer primary key autoincrement, name text, age integer) ;
3>外键约束
利用外键约束能够用来创建表与表之间的联系,一般是多属性的一个表引用另外一个表的主键。
外键的通常状况是:一张表的某个字段,引用着另外一张表的主键字段
新建一个外键
create table t_student (id integer primary key autoincrement, name text, age integer, class_id integer, constraint fk_student_class foreign key (class) references t_class (id));
constraint fk_student_class foreign key 外键约束名 (class) t_student表的class字段 references t_class (id) 另外一张表的主键字段
t_student表中有一个叫作fk_t_student_class_id_t_class_id的外键
这个外键的做用是用t_student表中的class_id字段引用t_class表的id字段
外键约束 还有级联这个功能
1)嵌套查询
SELECT id FROM t_food_type WHERE name = '粤菜'; //先查找粤菜的ID SELECT * From t_food WHERE food_type_id = (SELECT id FROM t_food_type WHERE name = '粤菜' );//经过粤菜的ID查找全部粤菜的信息
2)多表查询
SELECT * from t_food, food_type_id; //会查询出两个表笛卡尔积的内容 //咱们须要筛选两边ID相同的信息(须要用到起别名) SELECT * From t_food f,t_food_type tf WHERE f.food_type_id = tf.id //继续筛选粤菜的信息 SELECT * From t_food f,t_food_type tf WHERE f.food_type_id = tf.id AND tf.name = ‘粤菜‘;
使用limit能够精确地控制查询结果的数量,好比每次只查询10条数据
select * from 表名 limit 数值1, 数值2 ;
limit经常使用来作分页查询,好比每页固定显示5条数据,那么应该这样取数据
第1页:limit 0, 5
第2页:limit 5, 5
第3页:limit 10, 5
…
第n页:limit 5*(n-1), 5
eg:
select * from t_student limit 4, 8 ; 能够理解为:跳过最前面4条语句,而后取8条记录 select * from t_student limit 7 ; 至关于select * from t_student limit 0, 7 ; 表示取最前面的7条记录
1)使用LIKE代替=
2)模糊查询字符的格式 '%xxxx%'
3)当使用NSString语句的时候,%是转义字符。因此咱们用%%就代替%,意思就是表示这个%不是转义支付
SELECT name,price FROM t_shop WHERE name LIKE '%李%' OR price LIKE '%2%';
FMDB以OC的方式封装了SQLite的C语言API
FMDB的优势
使用起来更加面向对象,省去了不少麻烦、冗余的C语言代码
对比苹果自带的Core Data框架,更加轻量级和灵活
提供了多线程安全的数据库操做方法,有效地防止数据混乱
FMDB的github地址
https://github.com/ccgus/fmdb
1)FMDatabase
一个FMDatabase对象就表明一个单独的SQLite数据库
用来执行SQL语句
2)FMResultSet
使用FMDatabase执行查询后的结果集
3)FMDatabaseQueue
用于在多线程中执行多个查询或更新,它是线程安全的
FMDatabase *db = [FMDatabase databaseWithPath:path]; if (![db open]) { NSLog(@"数据库打开失败!"); }
空字符串@"" 会在临时目录建立一个空的数据库。当FMDatabase链接关闭时,数据库文件也被删除
nil会建立一个内存中临时数据库,当FMDatabase链接关闭时,数据库会被销毁
在FMDB中,除查询之外的全部操做,都称为“更新”
create、drop、insert、update、delete等
使用executeUpdate:方法执行更新
- (BOOL)executeUpdate:(NSString*)sql, ... - (BOOL)executeUpdateWithFormat:(NSString*)format, ... - (BOOL)executeUpdate:(NSString*)sql withArgumentsInArray:(NSArray *)arguments
示例 FMDB中SQL语句字符串不须要加''
//插入数据 [db executeUpdate:@"insert into test (a, b, c, d, e) values (?, ?, ?, ?, ?)" , @"hi'", // look! I put in a ', and I'm not escaping it! [NSString stringWithFormat:@"number %d", i], [NSNumber numberWithInt:i], [NSDate date], [NSNumber numberWithFloat:2.2f]]; } //更新数据 [db executeUpdate:@"UPDATE t_student SET age = ? WHERE name = ?;", @20, @"Jack"]
查询方法
- (FMResultSet *)executeQuery:(NSString*)sql, ... - (FMResultSet *)executeQueryWithFormat:(NSString*)format, ... - (FMResultSet *)executeQuery:(NSString *)sql withArgumentsInArray:(NSArray *)arguments
示例.遍历结果集
FMResultSet *rs = [db executeQuery:@"SELECT * FROM t_student"]; // 遍历结果集 while ([rs next]) { NSString *name = [rs stringForColumn:@"name"]; int age = [rs intForColumn:@"age"]; double score = [rs doubleForColumn:@"score"]; }
模糊查询
查询语句中%%xxx%%表明包含xx字段,%x表明xx开头字段,x%表明xx结尾字段 +(NSArray*)queryFuzzyContactInfo:(NSString *)item { FMDatabase *database = [FMDatabase databaseWithPath:SQLITEPATH1]; if (![database open]) { NSLog(@"数据库打开失败"); } NSMutableArray *array = [NSMutableArray arrayWithCapacity:0]; FMResultSet *resultSet = [database executeQuery:@"select * from mysql1 WHERE name like '%%%@%%'",item]; while ([resultSet next]) { NSString * name = [resultSet stringForColumn:@"name"]; } return array; }
FMDatabase这个类是线程不安全的,若是在多个线程中同时使用一个FMDatabase实例,会形成数据混乱等问题
为了保证线程安全,FMDB提供方便快捷的FMDatabaseQueue类
1)FMDatabaseQueue的建立
FMDatabaseQueue *queue = [FMDatabaseQueue databaseQueueWithPath:path];
2)简单使用
[queue inDatabase:^(FMDatabase *db) { [db executeUpdate:@"INSERT INTO t_student(name) VALUES (?)", @"Jack"]; [db executeUpdate:@"INSERT INTO t_student(name) VALUES (?)", @"Rose"]; [db executeUpdate:@"INSERT INTO t_student(name) VALUES (?)", @"Jim"]; FMResultSet *rs = [db executeQuery:@"select * from t_student"]; while ([rs next]) { // … } }];
3)使用事务
[queue inTransaction:^(FMDatabase *db, BOOL *rollback) { [db executeUpdate:@"INSERT INTO t_student(name) VALUES (?)", @"Jack"]; [db executeUpdate:@"INSERT INTO t_student(name) VALUES (?)", @"Rose"]; [db executeUpdate:@"INSERT INTO t_student(name) VALUES (?)", @"Jim"]; FMResultSet *rs = [db executeQuery:@"select * from t_student"]; while ([rs next]) { // … } }]; 事务回滚 *rollback = YES;