// // ViewController.m // 01-CoreData基本使用 // // Created by mac on 16/5/4. // Copyright © 2016年 mac. All rights reserved. // #import "ViewController.h" #import <CoreData/CoreData.h> #import "User.h" @interface ViewController () @end @implementation ViewController { NSManagedObjectContext *_managerObjectContext; } - (void)viewDidLoad { [super viewDidLoad]; [self openDataBase]; // [self addUser]; // [self searchUserWithUserID:55]; [self updateUser]; [self deleteUser]; NSLog(@"---------%@", NSHomeDirectory()); } /** * 1. 打开数据库 */ - (void)openDataBase { //1. 创建数据库文件 NSString *dataBaseFilePath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/User.sqlite"]; NSURL *dataBaseFileUrl = [NSURL fileURLWithPath:dataBaseFilePath]; //2. 创建模型描述文件 /** a. 创建模型描述文件 b. */ //3. 获取模型描述文件路径 NSURL *entityFileURL = [[NSBundle mainBundle] URLForResource:@"UserModel" withExtension:@"momd"]; NSLog(@"%@", entityFileURL); //4. 通过模型描述文件 来创建pcs(持久化存储坐标) NSManagedObjectModel *model = [[NSManagedObjectModel alloc] initWithContentsOfURL:entityFileURL];// model NSPersistentStoreCoordinator *pcs = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:model]; //5.创建实体数据库文件 NSError *error = nil; [pcs addPersistentStoreWithType:NSSQLiteStoreType configuration:NULL URL:dataBaseFileUrl options:nil error:&error]; if (error) { NSLog(@"失败"); } else { NSLog(@"成功"); } _managerObjectContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType]; _managerObjectContext.persistentStoreCoordinator = pcs; } /** * 2. 添加数据 */ - (void)addUser { /* //1. 创建MO对象并且添加到objectContext // User *user1 = [[User alloc] init]; User *user1 = (User *)[NSEntityDescription insertNewObjectForEntityForName:@"User" inManagedObjectContext:_managerObjectContext]; //2. 使用context 将数据存储到数据库 user1.username = @"zhangsan"; user1.password = @"1234"; user1.user_id = @1001; //3. 使用context将数据存储到数据库 NSError *error = nil; [_managerObjectContext save:&error]; if (error) { NSLog(@"保存失败%@", error); } else { NSLog(@"保存成功"); } */ for (int i=0; i<100; i++) { User *user = [NSEntityDescription insertNewObjectForEntityForName:@"User" inManagedObjectContext:_managerObjectContext]; user.username = [NSString stringWithFormat:@"user%i", i]; user.user_id = @(i); user.password = @"123456"; //使用context将数据存储到数据库 NSError *error = nil; [_managerObjectContext save:&error]; if (error) { NSLog(@"保存失败"); } else { NSLog(@"保存成功"); } } } /** * 3. 查询数据 */ - (void)searchUserWithUserID:(NSInteger)userID { //1. 构建查询请求 NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"User"]; //2. 设置查询的条件 //3. 使用谓词来设定查询的条件 NSPredicate *predicate = [NSPredicate predicateWithFormat:@"user_id>%li",userID]; fetchRequest.predicate = predicate; //4. 执行查询请求 NSError *error = nil; NSArray *resultArray = [_managerObjectContext executeFetchRequest:fetchRequest error:&error]; //5. 处理查询结果 if (error) { NSLog(@"查询出错%@", error); } else { //查询成功 for (User *user in resultArray) NSLog(@"name = %@, password = %@, user_id = %@", user.username, user.password, user.user_id); } } /** * 4. 修改数据 */ - (void)updateUser { //0. 将密码修改为88888888 //1. 从数据库中查到需要修改的数据 NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"User"]; NSPredicate *predicate = [NSPredicate predicateWithFormat:@"user_id=88"]; request.predicate = predicate; //2. 执行查询请求 NSArray *array = [_managerObjectContext executeFetchRequest:request error:nil]; //3. 修改这些数据 for (User *user in array) { user.password = @"88888888"; } //4. 将修改后的数据保存到数据库 [_managerObjectContext save:nil]; } /** * 5. 删除数据 */ - (void)deleteUser { //1. 查询需要删除的用户对象 NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"User"]; NSPredicate *predicate = [NSPredicate predicateWithFormat:@"user_id=86"]; request.predicate = predicate; //2. 执行查询的请求 NSArray *array = [_managerObjectContext executeFetchRequest:request error:nil]; //3. 修改数据 for (User *user in array) { [_managerObjectContext deleteObject:user]; } //4. 将修改后的数据保存到数据库 [_managerObjectContext save:nil]; } @end
时间: 2024-10-24 13:57:33