core data CRUD简单操作
2015-06-26 15:18
399 查看
// // ViewController.m // MyCoreData // // Created by 麦子 on 15/7/11. // Copyright (c) 2015年 麦子. All rights reserved. // #import "ViewController.h" #import "AppDelegate.h" #import "Person.h" #import "Card.h" @interface ViewController (){ AppDelegate *app; NSManagedObjectContext *context; // 数据库API操作类 Person *person; Card *card; } @end /** 打开CoreData的SQL语句输出开关 1.打开Product,点击EditScheme... 2.点击Arguments,在ArgumentsPassed On Launch中添加2项 1> -com.apple.CoreData.SQLDebug 2> 1 */ @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // 创建数据库 app = [UIApplication sharedApplication].delegate; context = app.managedObjectContext; NSLog(@"%@--",[app applicationDocumentsDirectory]); // [self addData]; [self queryData]; // [self updateData]; [self delData]; } #pragma mark --- 添加数据 - (void)addData{ // 人群表 person = [NSEntityDescription insertNewObjectForEntityForName:@"Person" inManagedObjectContext:context]; // 组装数据 person.name = @"小红"; person.age = [NSNumber numberWithInt:25]; // 身份证表 card = [NSEntityDescription insertNewObjectForEntityForName:@"Card" inManagedObjectContext:context]; card.no = @"123456789"; person.card = card; // 同步到数据库 NSError *error = nil; BOOL flag = [context save:&error]; // 这一步才是同步,也就是commit操作 if (flag) { NSLog(@"数据保存成功。。。。"); }else{ NSLog(@"%@",error.description); } } #pragma mark ----- 查询 - (void)queryData{ // 找到需要的表 NSEntityDescription *personDes = [NSEntityDescription entityForName:@"Person" inManagedObjectContext:context]; // 初始化一个查询请求 NSFetchRequest *request = [[NSFetchRequest alloc] init]; // 设置要查询的实体 [request setEntity:personDes]; // 条件设置 NSPredicate *pred = [NSPredicate predicateWithFormat:@" name like %@",@"小*"]; // [request setPredicate:pred]; // 运行SQL NSError *err; NSArray *dataArray = [context executeFetchRequest:request error:&err]; if (dataArray) { for (Person *personEntity in dataArray) { NSLog(@"person 数据--%@--%d-%@",personEntity.name,[personEntity.age intValue],personEntity.card.no); } } if (err) { NSLog(@"%@--",err.description); } } #pragma mark 修改 - (void)updateData{ NSEntityDescription *pEntity = [NSEntityDescription entityForName:@"Person" inManagedObjectContext:context]; NSFetchRequest *request = [[NSFetchRequest alloc] init]; request.entity = pEntity; // NSPredicate *pred = [NSPredicate predicateWithFormat:@"name like %@",@"麦子*"]; // [request setPredicate:pred]; NSError *err = nil; NSArray *arryData = [context executeFetchRequest:request error:&err]; int count = 0; for (Person *personEntity in arryData) { NSLog(@"person 修改--名字:%@--年龄:%d--身份证:%@",personEntity.name,[personEntity.age intValue],personEntity.card.no); ++count; personEntity.name = [NSString stringWithFormat:@"哈哈%d",count]; personEntity.card.no = @"110"; } if (err) { NSLog(@"%@",err.description); } // 修改后提交,就可以改变数据 bool flag = [context save:&err]; if (flag) { NSLog(@"修改成功"); }else{ NSLog(@"%@",err.description); } } #pragma mark -- 删除 - (void)delData{ NSEntityDescription *perDes = [NSEntityDescription entityForName:@"Person" inManagedObjectContext:context]; NSFetchRequest *request = [[NSFetchRequest alloc] init]; request.entity = perDes; NSPredicate *pre = [NSPredicate predicateWithFormat:@" name = %@",@"哈哈3"]; [request setPredicate:pre]; NSError *error = nil; NSArray *array = [context executeFetchRequest:request error:&error]; for (Person *personEntity in array) { NSLog(@"person 修改--名字:%@--年龄:%d--身份证:%@",personEntity.name,[personEntity.age intValue],personEntity.card.no); [context deleteObject:personEntity]; } // 删除,修改,添加, 都需要最后的这个方法,进行commit bool flag = [context save:&error]; if ([array count] > 0) { if (flag ) { NSLog(@"删除成功"); }else{ NSLog(@"%@",error.description); } } } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; } @end
Person 和 Card 关联。
相关文章推荐
- git常用命令
- java中的sleep()和wait()的区别
- 完成率,保留两位小数,double
- JAVA多线程编程(三)——线程的生命周期(sleep,join,interrupt)
- 文章标题
- aidl is missing
- SQLite3中的日期时间函数使用小结
- JVM学习笔记(二)------Java代码编译和执行的整个过程
- JVM学习笔记(二)------Java代码编译和执行的整个过程
- gitlab ActionView::Template::Error (undefined method `[]' for nil:NilClass): 500错误
- Spring整合RMI
- 合并两个有序数组
- MAC命令 和 vi命令
- JVM学习笔记(一)------基本结构
- 查找当前SQL Server下的Active Session正连接着哪个数据库
- 手机浏览器全屏模式
- JVM学习笔记(一)------基本结构
- 关于背景图片定位问题
- 66. Plus One
- javascript实现textarea中tab键的缩排处理方法