- 創(chuàng)建一個模型文件(相當(dāng)于創(chuàng)建了一個數(shù)據(jù)庫)
圖片.png
- 創(chuàng)建一個實體就相當(dāng)于創(chuàng)建了一張表(在attribut添加屬性)
圖片.png
- 創(chuàng)建一個實體類相當(dāng)于創(chuàng)建了一個模型類
圖片.png
- 獲取上下文
-(void)setupContext{
// 1.上下文 關(guān)聯(lián)Company.xcdatamodeld 模型文件
NSManagedObjectContext *context = [[NSManagedObjectContext alloc] init];
// 關(guān)聯(lián)模型文件
// 創(chuàng)建一個模型對象
// 傳一個nil 會把 bundle下的所有模型文件 關(guān)聯(lián)起來
NSManagedObjectModel *model = [NSManagedObjectModel mergedModelFromBundles:nil];
// 持久化存儲調(diào)度器
NSPersistentStoreCoordinator *store = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:model];
// 存儲數(shù)據(jù)庫的名字
NSError *error = nil;
// 獲取docment目錄
NSString *doc = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
// 數(shù)據(jù)庫保存的路徑
NSString *sqlitePath = [doc stringByAppendingPathComponent:@"company.sqlite"];
[store addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:[NSURL fileURLWithPath:sqlitePath] options:nil error:&error];
context.persistentStoreCoordinator = store;
self.context = context;
}
- 基本CRUD
(IBAction)addEmployee:(id)sender {
// 創(chuàng)建員工
Employee *emp1 = [NSEntityDescription insertNewObjectForEntityForName:@"Employee" inManagedObjectContext:self.context];
// 設(shè)置員工屬性
emp1.name = @"lisi";
emp1.age = @28;
emp1.height = @2.10;
//保存 - 通過上下文操作
NSError *error = nil;
[self.context save:&error];
if (!error) {
NSLog(@"success");
}else{
NSLog(@"%@",error);
}
}
//創(chuàng)建一個請求對象 (填入要查詢的表名-實體類)
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Employee"];
// 查找張三 并且身高大于1.8
// NSPredicate *pre = [NSPredicate predicateWithFormat:@"name=%@ AND height > %@",@"zhangsan",@(1.8)];// request.predicate = pre;
//排序 以身高進(jìn)行升序
// NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"height" ascending:NO];
// request.sortDescriptors = @[sort];
// 分頁查詢 總共13條數(shù)據(jù) 每頁顯示5條數(shù)據(jù)
//第一頁的數(shù)據(jù)
request.fetchLimit = 5;
request.fetchOffset = 10;
//讀取信息
NSError *error = nil;
NSArray *emps = [self.context executeFetchRequest:request error:&error];
if (!error) {
NSLog(@"emps: %@",emps);
for (Employee *emp in emps) {
NSLog(@"%@ %@ %@",emp.name,emp.age,emp.height);
}
}else{
NSLog(@"%@",error);
}
-(void)deleteEmployeeWithName:(NSString *)name{
// 刪除zhangsan
// 1.查找到zhangsan
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Employee"];
NSPredicate *pre = [NSPredicate predicateWithFormat:@"name=%@",name];
request.predicate = pre;
// 2.刪除zhangsan
NSArray *emps = [self.context executeFetchRequest:request error:nil];
for (Employee *emp in emps) {
NSLog(@"刪除員工的人 %@",emp.name);
[self.context deleteObject:emp];
}
// 3.用context同步下數(shù)據(jù)庫
//所有的操作暫時都是在內(nèi)存里浴栽,調(diào)用save 同步數(shù)據(jù)庫
[self.context save:nil];
}
(IBAction)updateEmployee:(id)sender {
// 把wangwu的身高更改成 1.7
// 1.查找wangwu
NSArray *emps = [self findEmployeeWithName:@"wangwu"];
// 2.更新身高
if (emps.count == 1) {
Employee *emp = emps[0];
emp.height = @1.7;
}
// 3.同步(保存)到數(shù)據(jù)
[self.context save:nil];
}
#pragma mark 模糊查詢
(IBAction)likeSearcher:(id)sender {
// 查詢
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Employee"];
// 過濾
// 1.查詢以wang開頭員工
//NSPredicate *pre = [NSPredicate predicateWithFormat:@"name BEGINSWITH %@",@"wang"];
// 2.以si 結(jié)尾
//NSPredicate *pre = [NSPredicate predicateWithFormat:@"name ENDSWITH %@",@"si"];
// 3.名字包含 g
//NSPredicate *pre = [NSPredicate predicateWithFormat:@"name CONTAINS %@",@"g"];
// 4.like 以si結(jié)尾
NSPredicate *pre = [NSPredicate predicateWithFormat:@"name like %@",@"li*"];
request.predicate = pre;
//讀取信息
NSError *error = nil;
NSArray *emps = [self.context executeFetchRequest:request error:&error];
if (!error) {
NSLog(@"emps: %@",emps);
for (Employee *emp in emps) {
NSLog(@"%@ %@ %@",emp.name,emp.age,emp.height);
}
}else{
NSLog(@"%@",error);
}
}