您的位置:首页 > 其它

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 关联。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  CRUD简单操作