先了解一下 Core Data 它相当于封装了sqlite
1.Core Data 是数据持久化存储的最佳方式,一般数据最终的存储类型可以是:SQLite数据库,XML,二进制,内存里,或自定义数据类型.core Data存储的路径格式是URL类型 ,sqlite 存储的路径格式是 string类型,在Mac OS X 10.5Leopard及以后的版本中,开发者也可以通过继承NSPersistentStore类以创建自定义的存储格式
2,Core Data的好处:能够合理管理内存,避免使用sql的麻烦,高效
3.构成:
(1)NSManagedObjectContext(被管理的数据上下文, 就是临时数据库增删改查就是操作的临时DB)
操作实际内容(操作持久层)
作用:插入数据,查询数据,删除数据
(2)NSManagedObjectModel(被管理的数据模型, 比如Student)
数据库所有表格或数据结构,包含各实体的定义信息
作用:添加实体的属性,建立属性之间的关系
操作方法:视图编辑器,或代码
(3)NSPersistentStoreCoordinator(持久化存储助理)
相当于数据库的连接器
作用:设置数据存储的名字,位置,存储方式,和存储时机
(4)NSManagedObject(被管理的数据记录)
相当于数据库中的表格记录
(5)NSFetchRequest(获取数据的请求)
相当于查询语句
(6)NSEntityDescription(实体结构)
相当于表格结构
(7)后缀为.xcdatamodeld的包
里面是.xcdatamodel文件,用数据模型编辑器编辑
<span style="font-size:18px;">//
// ViewController.m
// LessonCodeData
//
// Created by WDX on 15/10/9.
// Copyright (c) 2015年 wangdongxu. All rights reserved.
//
#import "ViewController.h"
#import "AppDelegate.h"
#import "Student.h"
@interface ViewController ()
#pragma mark - 属性, 被管理对象上下文(临时数据库)
@property (nonatomic, strong) NSManagedObjectContext *manmagerContext;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
#pragma mark - 添加
- (IBAction)addData:(id)sender {
// 创建实体描述
NSEntityDescription *studentED = [NSEntityDescription entityForName:@"Student" inManagedObjectContext:self.manmagerContext];
for (int i = 0; i < 10; i++) {
// 根据实体描述创建实体
Student *student = [[Student alloc] initWithEntity:studentED insertIntoManagedObjectContext:self.manmagerContext];
// 给实体赋值
student.name = [NSString stringWithFormat:@"东方红%d号", i + 1];
student.age = [NSNumber numberWithInt:(18 + i)];
student.sex = [NSString stringWithFormat:@"男"];
}
NSError *error = nil;
// 数据同步
[self.manmagerContext save:&error];
if (!error) {
NSLog(@"存储成功");
}
}</span>
<span style="font-size:18px;">#pragma mark - 查找
- (IBAction)findData:(id)sender {
// 从实体中查询
NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"Student"];
// 创建实体描述
NSEntityDescription *studentED = [NSEntityDescription entityForName:@"Student" inManagedObjectContext:self.manmagerContext];
// 设置查询实体
request.entity = studentED;
// 设置查询根据key和BOOL进行升序降序排列,key为排列条件,YES为升序,NO是降序
NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"age" ascending:YES];
request.sortDescriptors = @[sort];
// 设置查询,根据指定条件查询
// NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name = %@", @"</span><pre name="code" class="objc"><span style="font-size:18px;">东方2号"];
// 模糊查询,包含
// NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name CONTAINS %@", @"3"];
// 不知道的地方用*代替
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name LIKE %@",@"东方*"];
// 添加查询条件
request.predicate = predicate;
// 数组接收查询的数据
NSArray *array = [self.manmagerContext executeFetchRequest:request error:nil];
// 遍历数组,取出数据打印
for (Student *student in array) {
NSLog(@"姓名:%@, 性别:%@, 年龄:%@", student.name, student.sex, student.age);
}
}
</span>
<span style="font-size:18px;">#pragma mark - 修改
- (IBAction)changeData:(id)sender {
// 先查询
// 从实体中查询
NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"Student"];
// 创建实体描述
NSEntityDescription *studentED = [NSEntityDescription entityForName:@"Student" inManagedObjectContext:self.manmagerContext];
// 设置查询实体
request.entity = studentED;
// 设置查询,根据指定条件查询
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name CONTAINS %@", @"5"];
// 添加查询条件
request.predicate = predicate;
// 数组接收查询数据
NSArray *array = [self.manmagerContext executeFetchRequest:request error:nil];
// 遍历
for (Student *student in array) {
student.sex = @"女";
student.age = [NSNumber numberWithInt:999];
NSLog(@"姓名:%@, 性别:%@, 年龄:%@", student.name, student.sex, student.age);
}
// 数据同步
NSError *error = nil;
// 数据同步
[self.manmagerContext save:&error];
if (!error) {
NSLog(@"修改成功");
}
}
</span>
<span style="font-size:18px;">#pragma mark - 删除
- (IBAction)deleteData:(id)sender {
// 先查询
// 从实体中查询
NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"Student"];
// 创建实体描述
NSEntityDescription *studentED = [NSEntityDescription entityForName:@"Student" inManagedObjectContext:self.manmagerContext];
// 设置查询实体
request.entity = studentED;
// 设置查询,根据指定条件查询
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name CONTAINS %@", @"6"];
// 添加查询条件
request.predicate = predicate;
// 数组接收查询数据
NSArray *array = [self.manmagerContext executeFetchRequest:request error:nil];
for (Student *student in array) {
// 删除
[self.manmagerContext deleteObject:student];
}
// 数据同步
NSError *error = nil;
// 数据同步
[self.manmagerContext save:&error];
if (!error) {
NSLog(@"删除成功");
}
}
#pragma mark - 被管理对象上下文(临时数据库)的懒加载方法
- (NSManagedObjectContext *)manmagerContext
{
if (!_manmagerContext) {
// 获取AppDelegate当中的被管理对象上下文
_manmagerContext = ((AppDelegate *)[UIApplication sharedApplication].delegate).managedObjectContext;
}
return _manmagerContext;
}
@end</span>