iOS-常见的数据存储方式
2016-07-26 12:00
441 查看
iOS中常见的数据存储方式
Plist(NSArray\NSDictionary)Preference(偏好设置\NSUserDefaults)
NSCoding(NSKeyedArchiver\NSkeyedUnarchiver)
SQLite3 (C语言,轻量级)
Core Data(OC语言,底层基于SQLite,重量级)
前三种方法适合存储简单的数据类型
后面两种适合存储大量数据,但Core Data偏向于重量级的数据存储,其操作相比SQLite3更加繁琐
对于SQLite,如果单纯使用它,在数据查询方面会比较繁琐,建议使用一款对其封装的的数据存储FMDB( OC 语言,操作简单 )
一、Plist
NSString *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject; NSString *arrayPath = [path stringByAppendingPathComponent:@"array.plist"]; NSString *dictPath = [path stringByAppendingPathComponent:@"dict.plist"]; NSArray *array = @[@"a",@"b",@"c"]; [array writeToFile:arrayPath atomically:YES]; NSDictionary *dict = @{@"a":@1,@"b":@2,@"c":@3}; [dict writeToFile:dictPath atomically:YES];
二、Preference
// 存 // 存对象 NSArray *arr = @[@"a",@"b"]; [[NSUserDefaults standardUserDefaults] setObject:arr forKey:@"object"]; // 存值 [[NSUserDefaults standardUserDefaults] setValue:@"123" forKey:@"value"]; // 存bool值 [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"boolean"]; // 取 NSArray *array = [[NSUserDefaults standardUserDefaults] objectForKey:@"object"]; NSString *value = [[[NSUserDefaults standardUserDefaults] valueForKey:@"value"] stringValue];
三、NSCoding
如果是自定义对象的的归档和反归档,需要遵守NSCoding协议,并实现对应的方法NSString *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject; NSString *dictPath = [path stringByAppendingPathComponent:@"dict.plist"]; NSDictionary *dict = @{@"a":@1}; // 归档 [NSKeyedArchiver archiveRootObject:dict toFile:dictPath]; <!--此方法等同于下面两个方法--> /** * NSData *data = [NSKeyedArchiver archivedDataWithRootObject:dict]; * [data writeToFile:dictPath atomically:YES]; */ // 反归档 NSDictionary *dic = [NSKeyedUnarchiver unarchiveObjectWithFile:dictPath];
四、SQLite
SQLite是一款轻型的嵌入式数据库它占用资源非常的低,在嵌入式设备中,可能只需要几百K的内存就够了
它的处理速度比Mysql、PostgreSQL这两款著名的数据库都还快
数据库存储数据的步骤:
01、新建一张表(table) 02、添加多个字段(column,列,属性) 03、添加多行记录(row,record,每行存放多个字段对应的值)
SQLite将数据划分为以下几种存储类型:
integer : 整型值
real : 浮点值
text : 文本字符串
blob : 二进制数据(比如文件)
实际上SQLite是无类型的
就算声明为integer类型,还是能存储字符串文本(主键除外)
SQL语句
在程序运行过程中,要想操作(增删改查,CRUD)数据库中的数据,必须使用SQL语句SQL语句的特点
不区分大小写(比如数据库认为user和UsEr是一样的) 每条语句都必须以分号 ‘;’ 结尾 SQL中的常用关键字有: select、insert、update、delete、from、create、where、desc、order、by、group、table、alter、view、index等等
数据定义语句(DDL:Data Definition Language)
包括create和drop等操作
create table - 创建表 drop table - 删除表
数据操作语句(DML:Data Manipulation Language)
包括insert、update、delete等操作
insert - 添加 update - 修改 delete - 删除
数据查询语句(DQL:Data Query Language)
可以用于查询获得表中的数据
关键字select是DQL(也是所有SQL)用得最多的操作
select - 查找 where - 位置 order by - 排序 group by - 分组 desc - 降序 asc - 升序
条件语句
如果只想更新或者删除某些固定的记录,那就必须在DML语句后加上一些条件
条件语句的常见格式 where 字段 = 某个值 ; // 不能用两个 = where 字段 is 某个值 ; // is 相当于 = where 字段 != 某个值 ; where 字段 is not 某个值 ; // is not 相当于 != where 字段 > 某个值 ; where 字段1 = 某个值 and 字段2 > 某个值 ; // and相当于C语言中的 && where 字段1 = 某个值 or 字段2 = 某个值 ; // or 相当于C语言中的 ||
01、创表
格式create table 表名 (字段名1 字段类型1, 字段名2 字段类型2, …) ;
create table if not exists 表名 (字段名1 字段类型1, 字段名2 字段类型2, …) ;
示例
create table t_person (id integer, name text, age inetger, height real) ;
02、删表
格式drop table 表名 ;
drop table if exists 表名 ;
示例
drop table t_person ;
03、插入数据(insert)
格式insert into 表名 (字段1, 字段2, …) values (字段1的值, 字段2的值, …) ;
示例
insert into t_person (name, age,height) values (‘xiaoming’, 20, 1.75) ;
注意
数据库中的字符串内容应该用单引号 ’ 括住
04、更新数据(update)
格式update 表名 set 字段1 = 字段1的值, 字段2 = 字段2的值, … ;
示例
update t_person set name = ‘xiaohong’, age = 18;
注意
上面的示例会将t_person表中所有记录的name都改为xiaohong,age都改为18
05、删除数据(delete)
格式delete from 表名 ;
示例
delete from t_person ;
注意
上面的示例会将t_person表中所有记录都删掉
相关文章推荐
- iOS代理设计模式
- IOS 强制去除里面的警告
- iOS开发多线程atomic nonatomic区别
- iOS编程(双语版) - 视图 - Transform(转换)
- iOS 公用方法
- iOS编程(双语版) - 视图 - Transform(转换)
- ios利用单例传值
- iOS音频篇:AVPlayer的缓存实现
- iOS 仿YY直播心形动画 & 烟花动画
- ios10适配
- OC - 原生扫码(扫描二维码,条形码)
- 使用SDWebImage和YYImage下载高分辨率图,导致内存暴增的解决办法
- OC里的一些小套路
- iOS解决按钮短时间内多次点击只触发一次事件方法
- iOS (高仿印物App)TableView给力动画的简单实现(二)
- iOS9 Universal Link实现
- iOS与HTML5交互方法总结
- iOS开发—在@interface,@implementation和@property中变量的定义
- [Cordova/Phonegap] Cordova iOS 应用在第三方输入法的键盘弹出(点击输入框)时,页面不上移,导致输入框被键盘遮挡 的解决办法
- 移动开发中的仿真器与模拟器