文章标题
来源:互联网 发布:淘宝让买家改评价 编辑:程序博客网 时间:2024/06/03 17:51
//mergedModelFromBundles 传一个 nil, 默认在 mainbundle 里面查找相应的 coredata 模型文件
NSManagedObjectModel *model = [NSManagedObjectModel mergedModelFromBundles:nil];
//创建协调者 (coreData 数据模型\本地数据文件)//初始化时指定 coreData 的数据模型NSPersistentStoreCoordinator *coordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:model];NSString *path = [NSString stringWithFormat:@"%@/data.db",NSHomeDirectory()];NSLog(@"%@",path);//把 coreData 数据文件和本地模型文件关联[coordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:[NSURL fileURLWithPath:path] options:nil error:nil];//创建上下文对象,用于操作数据NSManagedObjectContext *context = [[NSManagedObjectContext alloc] init];//设置属性,指定协调者context.persistentStoreCoordinator = coordinator;/*********************通过 coreData 机制操作数据********************///添加数据//创建一个数据对象,通过NSEntityDescription 调用insertNewObjectForEntityForName 创建//参数一::实体名字,与 coreData 模型文件中的实体名字一致//参数二:上下文对象Student *stu = [NSEntityDescription insertNewObjectForEntityForName:@"Student" inManagedObjectContext:context];//对数据模型设置各种属性stu.name = @"陈冠希";stu.number = @5;stu.age = @35;//本地化,内存---->本地文件(ROM)//[context save:nil];//查找数据//查找条件类NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];//全部数据的查找 指定实体
// [fetchRequest setEntity:(NSEntityDescription *)[NSEntityDescription insertNewObjectForEntityForName:@”Student” inManagedObjectContext:context]];
fetchRequest.entity = [NSEntityDescription entityForName:@”Student” inManagedObjectContext:context];
//增加一个查询的谓词NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name like '刘德华'"];[fetchRequest setPredicate:predicate];//执行数据检索NSArray *array = [context executeFetchRequest:fetchRequest error:nil];for (Student *item in array) { NSLog(@"%@ %@ %@",item.name,item.number,item.age);}//修改数据 //查找到待修改的数据Student *stur = [array firstObject]; //重新赋值stur.age = @120; //由内存存放到硬盘[context save:nil];//删除数据 //查找数据Student *str = [array firstObject]; //删除数据[context deleteObject:str]; //同步磁盘[context save:nil];
还有第三方库的使用
//1.添加数据
//创建对象
Student *stu = [Student MR_createEntity];
//设置属性
stu.name = @”佛纳甘”;
stu.age = @18;
stu.number = @12457116;
//存储到本地
// [[NSManagedObjectContext MR_defaultContext] MR_saveToPersistentStoreAndWait];
//读取数据
// Student *stud = [Student MR_findFirst];
// NSLog(@”%@ %@”,stud.name,stud.age);
//取出所有元素
// NSArray *array = [Student MR_findAll];
// for (Student *item in array) {
// NSLog(@”%@ %@”,item.name,item.age);
// }
//根据指定条件查找数据
NSArray *myArray = [Student MR_findByAttribute:@”name” withValue:@”全智贤”];
for (Student *item in myArray) {
NSLog(@”%@ %@”,item.name,item.age);
}
//更改元素
// for (Student *item in myArray) {
// item.age = @35;
// [[NSManagedObjectContext MR_defaultContext] MR_saveToPersistentStoreAndWait];
// }
//删除数据
[[myArray firstObject] MR_deleteEntity];
[[NSManagedObjectContext MR_defaultContext] MR_saveToPersistentStoreAndWait];
- 文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题 文章标题 文章标题 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 无锁编程(五) - RCU(Read-Copy-Update)
- IOS UILabel
- LeetCode(52) Power of Two
- 按enter键提交表单
- leetcode | Merge Sorted Array
- 文章标题
- Android 位置服务——BaiduLocation的使用
- Myeclipse_Pro_GA_2014中的服务器视图(Servers)中如何去掉讨厌的MyEclipse Derby选项
- Android中运行的错误:java.lang.UnsatisfiedLinkError: Couldn't load locSDK3: findLibrary returned null.
- AngularJs 构建复杂应用(二)
- HDU 2412
- iOS程序内打开其他app
- We are 伐木累
- JAVA WEB 总结 Servlet篇