iOS中数据存储方式

1,Plist(NSDictionary\NSArray)

特点:只能存储系统自带的数据类型,不能存储自定义类型。

2,Preference偏好设置(NSUserDefaults)

特点:NSUserDefaults是一个单例,在整个程序中只有一个实例对象,它可以用于数据的永久存储,主要面向的是一些简单数据类型,如:NSNumber、NSString、NSArray、NSDictionary ...,使用场景:(1)判断是否第一次启动APP,(2)自动登陆。

3,NSCoding(NSkeyedArchiver\NSKeyedUnarchiver)

特点:可以存储自定义类型。

4,SQLite3,

特点:轻型的嵌入式数据库,占用资源比较低,处理速度比较快,可视化数据库管理工具Navicat

5,Core Data,

特点:对SQLite3一层面向对象的封装,本质还是要转换成对应的SQL语句去执行。

SQL语句的种类

SQL语句是不区分大小写,增查改删简称CRUD(create、retrive、update、delete)

1,数据定义语言(DDL data definition language)

包括create、drop、Alert (修改表) 创建表create Table 或 删除表drop table
Alert(修改表)在SQLite中只能实现只能实现部分功能,不能删除一列或已经存在的列名。

约束条件

1,简单约束

不能为空: not null
不能重复:unique
默认值:default

2,主键约束

主键可以是一个字段或多个字段
主键:primary key

例如

 create table  if not exists t_student  (name text, age integer, score real)
drop table if exists t_student
// 修改表名
 alert table 旧表面 rename to 新表名
// 新增属性
alter table 表名 add column 列名 数据类型 限定符
//添加约束
create table if not exists t_student (id integer primary key autoincrement, age integer,  sex text not null default '女')

2,数据操作语言(DML data manipulation language)

包括insert、delete、update等

常用条件语句

where 字段 = 某个值
where 字段 is 某个值 // is 等价于 =
where 字段 != 某个值
where 字段 is not 某个值 // is not 等价于 !=
where 字段 > 某个值
where 字段1 > 某个值 and 字段2 = 某个值
where 字段1 = 某个值 or 字段2 = 某个值

例如

insert into t_student (name, age, score) values ('xiaoming', 5, 90.0)
update t_student set score = 100 where name = 'xiaoming'

3,数据查询语言(DQL data query language)

包括select,常用关键字where、order by、group by、having等。

常用查询语句

// *代表所有字段,如果写具体字段则查询该字段个数,该字段为空,则会忽略。
count(*)
avg()
sum()
max()
min()

// 排序可以升序(asc) 降序(desc),可以用多个字段进行排序
order by
// 分页数值1表示跳过多少行,数值2表示取多少条
limit 数值1,数值2

例如

select * from t_student 
select name age from t_student
select * from t_student where age > 12
select avg(*) from t_student
select count(name) from t_student
select * from t_student order by age desc, score desc
select * from t_student limit 1,10 // 表示跳过1行取10条
select * from t_student limit 10 // 表示取前面10条
select * from t_student, t_boy where t_student.id = t_boy.user_id // 多表查询

// SQLite插入数据执行效率比较高的做法

  sqlite3_stmt *ppStmt = NULL;
    sqlite3_exec(_db,"begin;",0,0,0); // 开启事务
    unsigned long index = arc4random_uniform(count);
    NSString *name = arr[index];
    const char *sql = "insert into t_user (name, age, score) values (?,?,?)";
    sqlite3_prepare_v2(_db, sql, -1, &ppStmt, NULL);
    for (int i = 0; i < 10000; i++) {
// 绑定
        sqlite3_bind_text(ppStmt, 1, name.UTF8String, -1, SQLITE_TRANSIENT);
        sqlite3_bind_int(ppStmt, 2, arc4random_uniform(30));
        sqlite3_bind_int(ppStmt, 3, arc4random_uniform(100));
// 执行
        sqlite3_step(ppStmt);
// 重置
        sqlite3_reset(ppStmt);
    }
// 释放
    sqlite3_finalize(ppStmt);
// 提交事务
    sqlite3_exec(_db,"commit;",0,0,0);

// 查询数据效率高的做法

    NSMutableArray *models = nil;
    char *sql = "SELECT id, name, score FROM t_student;";
    // 1.准备查询
    sqlite3_stmt *stmt; // 用于提取数据的变量
    int result = sqlite3_prepare_v2(_db, sql, -1, &stmt, NULL);
    // 2.判断是否准备好
    if (SQLITE_OK == result) {
        models = [NSMutableArray array];
        // 准备好了
        while (SQLITE_ROW == sqlite3_step(stmt)) { // 提取到一条数据
            // 从stmt中取出提取到的数据
            int ID =  sqlite3_column_int(stmt, 0);
            const unsigned char * name = sqlite3_column_text(stmt, 1);
            double score = sqlite3_column_double(stmt, 2);
//            NSLog(@"%d %s %.1f", ID, name, score);
            
            // 创建模型
            IWStudent *stu = [[IWStudent alloc] init];
            stu.ID = ID;
            stu.name = [NSString stringWithUTF8String:name];
            stu.score = score;
            
            [models addObject:stu];
        }
    }
    

你可能感兴趣的:(iOS中数据存储方式)