1.在.xcdatamodeld中添加实体
2.添加相应实体类
3.导入(coredata.framework 在开始时已选择use core data 自动添加)
4.初始化响应信息
// 从应用程序包中加载模型文件
NSManagedObjectModel *model = [NSManagedObjectModel mergedModelFromBundles:nil];
//初始化持久化对象
NSPersistentStoreCoordinator *per=[[NSPersistentStoreCoordinator alloc]initWithManagedObjectModel:model];
//创建sqlight路径
NSString *path=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
NSURL *url=[NSURL fileURLWithPath:[path stringByAppendingPathComponent:@"person.data"]];
//添加持久化存储
NSError *error=nil;
NSPersistentStore *store = [per addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:url options:nil error:&error];
if (store == nil) { // 直接抛异常
[NSException raise:@"添加数据库错误" format:@"%@", [error localizedDescription]];
NSLog(@"打开数据库失败!---%@",error.localizedDescription);
}else{
NSLog(@"打开数据库成功!");
// 初始化上下文,设置persistentStoreCoordinator属性
self.context = [[NSManagedObjectContext alloc] init];
_context.persistentStoreCoordinator = per;
}
现在就可以根据上下文context进行增删改查操作了
5.添加数据
//传入上下文,初始化一个person
// NSManagedObject *person=[NSEntityDescription insertNewObjectForEntityForName:@"Person" inManagedObjectContext:self.context];
// [person setValue:@"张三" forKeyPath:@"name"];
// [person setValue:[NSNumber numberWithInt:25] forKeyPath:@"age"];
//
// //初始化card
// NSManagedObject *card=[NSEntityDescription insertNewObjectForEntityForName:@"Card" inManagedObjectContext:self.context];
// [card setValue:@"1234567" forKeyPath:@"no"];
//
// //创建card和person关系
// [person setValue:card forKeyPath:@"Card"];
// 2)model方式
Person *person=[NSEntityDescription insertNewObjectForEntityForName:@"Person" inManagedObjectContext:self.context];
person.name=@"张三";
person.age=[NSNumber numberWithInt:25];
Card *card=[NSEntityDescription insertNewObjectForEntityForName:@"Card" inManagedObjectContext:self.context];
card.no=@"123456";
person.card=card;
//利用上下文保存数据
NSError *error=nil;
BOOL result=[self.context save:&error];
if (result) {
NSLog(@"插入成功");
}else{
NSLog(@"插入失败");
}
6.查询数据
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Person" inManagedObjectContext:self.context];
[fetchRequest setEntity:entity];
// 设置条件过滤时,数据库SQL语句中的%要用*来代替
// NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name like %@", @"张"];
// [fetchRequest setPredicate:predicate];
// 排序
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"age"
ascending:YES];
[fetchRequest setSortDescriptors:[NSArray arrayWithObjects:sortDescriptor, nil]];
NSError *error = nil;
NSArray *fetchedObjects = [self.context executeFetchRequest:fetchRequest error:&error];
if (fetchedObjects == nil) {
NSLog(@"查询失败");
}else
{
for (Person *obj in fetchedObjects) {
Card *car=(Card *)(obj.card);
NSLog(@"name:%@",car.no);
}
}
7.删除数据
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Person" inManagedObjectContext:self.context];
[fetchRequest setEntity:entity];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"age=%@", [NSNumber numberWithInt:25]];
[fetchRequest setPredicate:predicate];
NSError *error = nil;
NSArray *fetchedObjects = [self.context executeFetchRequest:fetchRequest error:&error];
if (fetchedObjects != nil) {
for (Person *obj in fetchedObjects) {
[self.context deleteObject:obj];
}
NSError *error=nil;
[self.context save:&error];
if (error) {
NSLog(@"删除错误");
}else {
NSLog(@"删除成功");
}
}
原文:http://www.cnblogs.com/zhangkk/p/4611207.html