coredata

#import "ViewController.h"#import#import "Teacher.h"

#import "Student.h"

@interface ViewController ()

- (IBAction)insert:(UIButton *)sender;

- (IBAction)delete:(UIButton *)sender;

- (IBAction)update:(UIButton *)sender;

- (IBAction)select:(UIButton *)sender;

//關聯上下文,一切的增刪該查操作有context進行

@property(nonatomic,strong)NSManagedObjectContext *context;

@end

@implementation ViewController

- (void)viewDidLoad {

[super viewDidLoad];

self.context =[[NSManagedObjectContext alloc]initWithConcurrencyType:NSMainQueueConcurrencyType];

//關聯數據庫

//數據庫的格式為momd

NSManagedObjectModel *model =[[NSManagedObjectModel alloc]initWithContentsOfURL:[[NSBundle mainBundle] URLForResource:@"Model" withExtension:@"momd"]];

//通過文件模型去初始化數據持久化助理

NSPersistentStoreCoordinator *store =[[NSPersistentStoreCoordinator alloc]initWithManagedObjectModel:model];

//創(chuàng)建文件路徑

NSString *filePath =[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject stringByAppendingPathComponent:@"model.sqlite"];

//關聯路徑

//如果需要版本迭代的話,那么要在options設置@{NSMigratePersistentStoresAutomaticallyOption:@YES,NSInferMappingModelAutomaticallyOption:@YES}并且要在一開始的時候就需要設置

[store addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:[NSURL fileURLWithPath:filePath ] options:@{NSMigratePersistentStoresAutomaticallyOption:@YES,NSInferMappingModelAutomaticallyOption:@YES} error:nil];

self.context.persistentStoreCoordinator =store;

NSLog(@"%@",filePath);

// Do any additional setup after loading the view, typically from a nib.

}

- (void)didReceiveMemoryWarning {

[super didReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}

//插入

- (IBAction)insert:(UIButton *)sender {

//創(chuàng)建一個學生

Student *stu1 = [NSEntityDescription insertNewObjectForEntityForName:@"Student" inManagedObjectContext:self.context];

//coredata的使用所有的增刪改操作完成之后需要保存一下

stu1.name =@"國棟";

stu1.age? =@18;

Student *stu2 = [NSEntityDescription insertNewObjectForEntityForName:@"Student" inManagedObjectContext:self.context];

//coredata的使用所有的增刪改操作完成之后需要保存一下

stu2.name =@"張凡";

stu2.age? =@20;

Student *stu3 = [NSEntityDescription insertNewObjectForEntityForName:@"Student" inManagedObjectContext:self.context];

//coredata的使用所有的增刪改操作完成之后需要保存一下

stu3.name =@"張沖";

stu3.age? =@22;

Student *stu4 = [NSEntityDescription insertNewObjectForEntityForName:@"Student" inManagedObjectContext:self.context];

//coredata的使用所有的增刪改操作完成之后需要保存一下

stu4.name =@"張華";

stu4.age? =@24;

Teacher *teacher1 =[NSEntityDescription insertNewObjectForEntityForName:@"Teacher" inManagedObjectContext:self.context];

teacher1.name =@"太皇太后";

teacher1.cosure=@"ios";

//? ? teacher1.myStudent =[NSSet setWithObjects:stu1,stu2,stu3,stu4, nil];

[teacher1 addMyStudent:[NSSet setWithObjects:stu1,stu2,stu3,stu4, nil]];

//? ? NSLog(@"集合的內容%@",teacher1);

[self.context save:nil];

}

//刪除

- (IBAction)delete:(UIButton *)sender {

NSFetchRequest *request =[NSFetchRequest fetchRequestWithEntityName:@"Student"];

NSPredicate *pre =[NSPredicate predicateWithFormat:@"name LIKE %@",@"國*"];

request.predicate=pre;

NSArray *array=[self.context executeFetchRequest:request error:nil];

for (Student *stu in array) {

[self.context deleteObject:stu];

[self.context save:nil];

}

}

//跟新

- (IBAction)update:(UIButton *)sender {

//1.創(chuàng)建查詢提

NSFetchRequest *requset =[NSFetchRequest fetchRequestWithEntityName:@"Student"];

//2. 設置查詢條件(這一步可有可無,根據需求來決定)

NSPredicate *pre =[NSPredicate predicateWithFormat:@"name =%@",@"張凡"];

//3.用請求體點出條件.

requset.predicate=pre;

NSArray *array=? [self.context executeFetchRequest:requset error:nil];

for (Student *stu in array) {

//修改

stu.name =@"張展";

[self.context save:nil];

//NSLog(@"%@,%@",stu.name,stu.age);

}

}

//查詢

- (IBAction)select:(UIButton *)sender {

/**

*1.創(chuàng)建查詢提

2. 設置查詢條件(這一步可有可無,根據需求來決定)

2.1 查詢:經常用到的有三種,第一種直接查詢.(name=張三)

第二種模糊查? NSPredicate *predicate =[NSPredicate predicateWithFormat:@"name CONTAINS %@",@"張"];

第三種:以什么結尾或以聲明開頭: NSPredicate *pre =[NSPredicate predicateWithFormat:@"name LIKE %@",@"*棟"];

用請求體點出條件.

3. 用context去執(zhí)行查詢請求 并用數組接受

*/

//nsfetchrequest

//創(chuàng)建查詢學生表的請求體

//? ? NSFetchRequest *request =[NSFetchRequest fetchRequestWithEntityName:@"Student"];

//? ? //(第一種)查詢年齡為20歲的人

//? ? /** 謂詞*/

//? ? NSPredicate *predicate =[NSPredicate predicateWithFormat:@"age =%@",@20];

//? ? //加入謂詞條件

//? ? request.predicate =predicate;

//****************************

//(第二種)查詢姓張的人

//? ? NSPredicate *predicate =[NSPredicate predicateWithFormat:@"name CONTAINS %@",@"張"];

//? ? request.predicate=predicate;

//(第三種)查詢名子以棟結尾的人

//? ? NSPredicate *pre =[NSPredicate predicateWithFormat:@"name LIKE %@",@"*棟"];

//? ? request.predicate=pre;

//********************

//(第四種)按年齡排序

//? ? NSSortDescriptor *sort =[NSSortDescriptor sortDescriptorWithKey:@"age" ascending:YES];

//? ? ? ? NSPredicate *predicate =[NSPredicate predicateWithFormat:@"name CONTAINS %@",@"張"];

//? ? ? ? request.predicate=predicate;

//? ? request.sortDescriptors=@[sort];

//執(zhí)行查詢操作

//? NSArray *array=? [self.context executeFetchRequest:request error:nil];

//? ? for (Student *stu in array) {

//? ? ? ? NSLog(@"%@ %@ %@",stu.name,stu.age,stu.number);

//? ? }

//查詢老師表

NSFetchRequest *request =[NSFetchRequest fetchRequestWithEntityName:@"Teacher"];

NSArray *array =[self.context executeFetchRequest:request error:nil];

for (Teacher *tea in array) {

for (Student *stu in tea.myStudent) {

NSLog(@"%@的學生 %@ %@ %@",tea.name,stu.name,stu.age,stu.number);

}

}

}

@end

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容