CoreData的使用
2015-11-01 14:40
239 查看
(void)openDB
{
// 创建数据库
// 1. 实例化数据模型(将所有定义的模型都加载进来)
// merge——合并
NSManagedObjectModel *model = [NSManagedObjectModel mergedModelFromBundles:nil];
// 2. 实例化持久化存储调度,要建立起桥梁,需要模型
NSPersistentStoreCoordinator *store = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:model];
// 3. 添加一个持久化的数据库到存储调度
// 3.1 建立数据库保存在沙盒的URL
NSArray *docs = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *path = [docs[0] stringByAppendingPathComponent:@”my.db”];
NSURL *url = [NSURL fileURLWithPath:path];
// 3.2 打开或者新建数据库文件
// 如果文件不存在,则新建之后打开
// 否者直接打开数据库
NSError *error = nil;
[store addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:url options:nil error:&error];
if (error) {
NSLog(@”打开数据库出错 - %@”, error.localizedDescription);
} else {
NSLog(@”打开数据库成功!”);
_context = [[NSManagedObjectContext alloc] init];
_context.persistentStoreCoordinator = store;
}
}
(void)addPerson
{
// 1. 实例化并让context“准备”将一条个人记录增加到数据库
Person *p = [NSEntityDescription insertNewObjectForEntityForName:@”Person” inManagedObjectContext:_context];
// 2. 设置个人信息
p.name = @”张老头”;
p.age = @10;
p.phoneNo = @”100”;
p.image = UIImagePNGRepresentation([UIImage imageNamed:@”头像1”]);
// 3. 新增书,实例化并通知上下文准备加书
Book *b = [NSEntityDescription insertNewObjectForEntityForName:@”Book” inManagedObjectContext:_context];
b.name = @”太极真经”;
b.price = @20000.99;
b.author = @”太极忽悠”;
Book *b2 = [NSEntityDescription insertNewObjectForEntityForName:@”Book” inManagedObjectContext:_context];
b2.name = @”一阳神功”;
b2.price = @0.99;
b2.author = @”老忽悠”;
NSSet *bookSet = [NSSet setWithObjects:b, b2, nil];
p.books = bookSet;
// 3. 保存(让context保存当前的修改)
if ([_context save:nil]) {
NSLog(@”新增成功”);
} else {
NSLog(@”新增失败”);
}
}
(void)removePerson
{
// 1. 实例化查询请求
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@”Person”];
// 2. 设置谓词条件
request.predicate = [NSPredicate predicateWithFormat:@”name = ‘张老头’”];
// 3. 由上下文查询数据
NSArray *result = [_context executeFetchRequest:request error:nil];
// 4. 输出结果
for (Person *person in result) {
NSLog(@”%@ %@ %@”, person.name, person.age, person.phoneNo);
// 删除一条记录
[_context deleteObject:person];
break;
}
// 5. 通知_context保存数据
if ([_context save:nil]) {
NSLog(@”删除成功”);
} else {
NSLog(@”删除失败”);
}
}
(void)updatePerson
{
// 1. 实例化查询请求
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@”Book”];
// 2. 设置谓词条件
request.predicate = [NSPredicate predicateWithFormat:@”author CONTAINS ‘忽悠’”];
// 3. 由上下文查询数据
NSArray *result = [_context executeFetchRequest:request error:nil];
// 4. 输出结果
for (Book *book in result) {
NSLog(@”%@ %@ %@”, book.name, book.author, book.price);
// 更新书名
book.name = @”西游记”;
}
// 通知上下文保存保存
[_context save:nil];
}
(void)allPersons
{
// 1. 实例化一个查询(Fetch)请求
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@”Person”];
// 3. 条件查询,通过谓词来实现的
// request.predicate = [NSPredicate predicateWithFormat:@”age < 60 && name LIKE ‘*五’”];
// 在谓词中CONTAINS类似于数据库的 LIKE ‘%王%’
// request.predicate = [NSPredicate predicateWithFormat:@”phoneNo CONTAINS ‘1’”];
// 如果要通过key path查询字段,需要使用%K
// request.predicate = [NSPredicate predicateWithFormat:@”%K CONTAINS ‘1’”, @”phoneNo”];
// 直接查询字表中的条件
// 2. 让_context执行查询数据
NSArray *array = [_context executeFetchRequest:request error:nil];
for (Person *p in array) {
NSLog(@”%@ %@ %@”, p.name, p.age, p.phoneNo);
// 在CoreData中,查询是懒加载的
// 在CoreData本身的SQL查询中,是不使用JOIN的,不需要外键
// 这种方式的优点是:内存占用相对较小,但是磁盘读写的频率会较高
for (Book *b in p.books) {
NSLog(@”%@ %@ %@”, b.name, b.price, b.author);
}
}
// for (Book *b in array) {
// NSLog(@”%@ %@ %@”, b.name, b.price, b.author);
// }
}
(void)queryAllbook
{
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@”Book”];
NSArray *arr = [_context executeFetchRequest:request error:nil];
for (Book *p in arr)
{
NSLog(@”%@ %@ %@”, p.name, p.price, p.author);
// 在CoreData中,查询是懒加载的
// 在CoreData本身的SQL查询中,是不使用JOIN的,不需要外键
// 这种方式的优点是:内存占用相对较小,但是磁盘读写的频率会较高
for (Person *b in p.persons) {
NSLog(@”%@ %@ %@”, b.name, b.age, b.phoneNo);
}
}
}
{
// 创建数据库
// 1. 实例化数据模型(将所有定义的模型都加载进来)
// merge——合并
NSManagedObjectModel *model = [NSManagedObjectModel mergedModelFromBundles:nil];
// 2. 实例化持久化存储调度,要建立起桥梁,需要模型
NSPersistentStoreCoordinator *store = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:model];
// 3. 添加一个持久化的数据库到存储调度
// 3.1 建立数据库保存在沙盒的URL
NSArray *docs = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *path = [docs[0] stringByAppendingPathComponent:@”my.db”];
NSURL *url = [NSURL fileURLWithPath:path];
// 3.2 打开或者新建数据库文件
// 如果文件不存在,则新建之后打开
// 否者直接打开数据库
NSError *error = nil;
[store addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:url options:nil error:&error];
if (error) {
NSLog(@”打开数据库出错 - %@”, error.localizedDescription);
} else {
NSLog(@”打开数据库成功!”);
_context = [[NSManagedObjectContext alloc] init];
_context.persistentStoreCoordinator = store;
}
}
(void)addPerson
{
// 1. 实例化并让context“准备”将一条个人记录增加到数据库
Person *p = [NSEntityDescription insertNewObjectForEntityForName:@”Person” inManagedObjectContext:_context];
// 2. 设置个人信息
p.name = @”张老头”;
p.age = @10;
p.phoneNo = @”100”;
p.image = UIImagePNGRepresentation([UIImage imageNamed:@”头像1”]);
// 3. 新增书,实例化并通知上下文准备加书
Book *b = [NSEntityDescription insertNewObjectForEntityForName:@”Book” inManagedObjectContext:_context];
b.name = @”太极真经”;
b.price = @20000.99;
b.author = @”太极忽悠”;
Book *b2 = [NSEntityDescription insertNewObjectForEntityForName:@”Book” inManagedObjectContext:_context];
b2.name = @”一阳神功”;
b2.price = @0.99;
b2.author = @”老忽悠”;
NSSet *bookSet = [NSSet setWithObjects:b, b2, nil];
p.books = bookSet;
// 3. 保存(让context保存当前的修改)
if ([_context save:nil]) {
NSLog(@”新增成功”);
} else {
NSLog(@”新增失败”);
}
}
(void)removePerson
{
// 1. 实例化查询请求
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@”Person”];
// 2. 设置谓词条件
request.predicate = [NSPredicate predicateWithFormat:@”name = ‘张老头’”];
// 3. 由上下文查询数据
NSArray *result = [_context executeFetchRequest:request error:nil];
// 4. 输出结果
for (Person *person in result) {
NSLog(@”%@ %@ %@”, person.name, person.age, person.phoneNo);
// 删除一条记录
[_context deleteObject:person];
break;
}
// 5. 通知_context保存数据
if ([_context save:nil]) {
NSLog(@”删除成功”);
} else {
NSLog(@”删除失败”);
}
}
(void)updatePerson
{
// 1. 实例化查询请求
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@”Book”];
// 2. 设置谓词条件
request.predicate = [NSPredicate predicateWithFormat:@”author CONTAINS ‘忽悠’”];
// 3. 由上下文查询数据
NSArray *result = [_context executeFetchRequest:request error:nil];
// 4. 输出结果
for (Book *book in result) {
NSLog(@”%@ %@ %@”, book.name, book.author, book.price);
// 更新书名
book.name = @”西游记”;
}
// 通知上下文保存保存
[_context save:nil];
}
(void)allPersons
{
// 1. 实例化一个查询(Fetch)请求
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@”Person”];
// 3. 条件查询,通过谓词来实现的
// request.predicate = [NSPredicate predicateWithFormat:@”age < 60 && name LIKE ‘*五’”];
// 在谓词中CONTAINS类似于数据库的 LIKE ‘%王%’
// request.predicate = [NSPredicate predicateWithFormat:@”phoneNo CONTAINS ‘1’”];
// 如果要通过key path查询字段,需要使用%K
// request.predicate = [NSPredicate predicateWithFormat:@”%K CONTAINS ‘1’”, @”phoneNo”];
// 直接查询字表中的条件
// 2. 让_context执行查询数据
NSArray *array = [_context executeFetchRequest:request error:nil];
for (Person *p in array) {
NSLog(@”%@ %@ %@”, p.name, p.age, p.phoneNo);
// 在CoreData中,查询是懒加载的
// 在CoreData本身的SQL查询中,是不使用JOIN的,不需要外键
// 这种方式的优点是:内存占用相对较小,但是磁盘读写的频率会较高
for (Book *b in p.books) {
NSLog(@”%@ %@ %@”, b.name, b.price, b.author);
}
}
// for (Book *b in array) {
// NSLog(@”%@ %@ %@”, b.name, b.price, b.author);
// }
}
(void)queryAllbook
{
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@”Book”];
NSArray *arr = [_context executeFetchRequest:request error:nil];
for (Book *p in arr)
{
NSLog(@”%@ %@ %@”, p.name, p.price, p.author);
// 在CoreData中,查询是懒加载的
// 在CoreData本身的SQL查询中,是不使用JOIN的,不需要外键
// 这种方式的优点是:内存占用相对较小,但是磁盘读写的频率会较高
for (Person *b in p.persons) {
NSLog(@”%@ %@ %@”, b.name, b.age, b.phoneNo);
}
}
}
相关文章推荐
- 杭电1282-回文
- Codeforces Round #328 (Div. 2) A. PawnChess 暴力
- 洛谷2633 王后万岁
- 一个轻量级的c编写的php接口平台框架
- 基于Jquery实现万圣节快乐特效
- (NO.00002)iOS游戏精灵战争雏形(七)
- Codeforces Round #328 D Super M(树的直径+树形DP)
- (NO.00002)iOS游戏精灵战争雏形(七)
- (NO.00002)iOS游戏精灵战争雏形(七)
- 洛谷2622 关灯问题II
- jsp的内置对象有下列这些
- iOS图片选择的优化与封装
- FMDB的使用
- Java线程池
- 用mysql的federated数据引擎和触发器实现跨服务器表数据同步
- SQLite3的使用
- asp.net之简易聊天室
- c++常量头文件的书写
- HDOJ 2534 Score(数学)
- 获得网络视频缩略图并且播放