#import "ViewController.h"正则表达式
#import "Person.h"//引入须要使用的实体数据库
#import <CoreData/CoreData.h>测试
@interface ViewController ()fetch
{spa
//管理对象上下文.net
NSManagedObjectContext *_context;3d
}orm
@end 对象
@implementation ViewControllerip
- (void)viewDidLoad {
[super viewDidLoad];
_context = [self createDBContext];
// [self addPersonWithName:@"测试员01" sex:@"男" age:@18];
// [self addPersonWithName:@"测试员02" sex:@"女" age:@16];
// [self addPersonWithName:@"测试员03" sex:@"男" age:@28];
NSLog(@"%@", [[self getPersonByName:@"测试员01"] valueForKey : @"sex"]);
// [self removePerson:@"测试员02"];
[self modifyPersonWithName:@"测试员03" sex:@"女" age:@100];
// Do any additional setup after loading the view, typically from a nib.
}
#pragma mark 1.建立管理上下文
- (NSManagedObjectContext *)createDBContext
{
NSManagedObjectContext *context;
//打开模型文件,参数为nil则打开包中全部模型文件并合并成一个
NSManagedObjectModel *model = [NSManagedObjectModel mergedModelFromBundles:nil];
//建立解析器NSPersistentStoreCoordinator
NSPersistentStoreCoordinator *storeCoordinator = [[NSPersistentStoreCoordinator alloc]initWithManagedObjectModel:model];
//建立数据库保存路径(文件目录)
NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
NSLog(@"%@", path);
path = [path stringByAppendingPathComponent:@"YC.db"];
//添加SQLite持久存储到解析器
NSError *error;
[storeCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:[NSURL fileURLWithPath:path] options:nil error:&error];
if (error) {
NSLog(@"数据库打开失败!错误: %@", error.localizedDescription);
}
else//数据库建立成功(打开成功)
{
context = [[NSManagedObjectContext alloc]initWithConcurrencyType:NSMainQueueConcurrencyType];
context.persistentStoreCoordinator = storeCoordinator;
NSLog(@"数据库打开成功!");
}
return context;
}
#pragma mark 2.插入数据
- (void)addPersonWithName : (NSString *)name sex : (NSString *)sex age : (NSNumber *)age
{
//建立实体对象
Person *person = [NSEntityDescription insertNewObjectForEntityForName:@"Person" inManagedObjectContext:_context];
person.name = name;
person.sex = sex;
person.age = age;
//保存上下文
NSError *error;
[_context save:&error];
if (error) {
NSLog(@"添加过程当中发送错误,错误: %@", error.localizedDescription);
}
else
{
NSLog(@"添加成功");
}
}
#pragma mark 3.查询数据
//NSPredicate(谓词)是一个Foundation的类,它指定数据被获取或者过滤的方式。它的语言就像SQL的where和正则表达式的交叉同样。提供了具备表现力的,天然语言界面来定义一个集合。(被搜索的逻辑条件)
//*zo* z, zo, zoo, zooo....
//zo+ zo, zoo, zooo, zoooo...
- (NSArray *)getPersonByName : (NSString *)name
{
//建立一个请求
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Person"];
request.predicate = [NSPredicate predicateWithFormat:@"%K=%@",@"name", name];
//上下文执行请求,获得查询结果
NSArray *array = [_context executeFetchRequest:request error:nil];
return array;
}
#pragma mark 4.删除数据
- (void)removePerson : (NSString *)name
{
Person *person = [[self getPersonByName:name] firstObject];
[_context deleteObject:person];
NSError *error;
[_context save:&error];
if (error) {
NSLog(@"删除过程当中发生错误:%@", error.localizedDescription);
}
else
{
NSLog(@"删除成功");
}
}
#pragma mark 修改数据
//必须首先提取出对应的实体对象,而后经过修改对象属性,最后保存
- (void)modifyPersonWithName : (NSString *)name sex : (NSString *)sex age : (NSNumber *)age{
Person *person = [[self getPersonByName:name] firstObject];
person.sex = sex;
person.age = age;
NSError *error;
[_context save:&error];
if (error) {
NSLog(@"修改过程当中发生错误: %@", error.localizedDescription);
}
else
{
NSLog(@"修改为功");
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end