#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;
//關(guān)聯(lián)上下文,一切的增刪該查操作有context進行
@property(nonatomic,strong)NSManagedObjectContext *context;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.context =[[NSManagedObjectContext alloc]initWithConcurrencyType:NSMainQueueConcurrencyType];
//關(guān)聯(lián)數(shù)據(jù)庫
//數(shù)據(jù)庫的格式為momd
NSManagedObjectModel *model =[[NSManagedObjectModel alloc]initWithContentsOfURL:[[NSBundle mainBundle] URLForResource:@"Model" withExtension:@"momd"]];
//通過文件模型去初始化數(shù)據(jù)持久化助理
NSPersistentStoreCoordinator *store =[[NSPersistentStoreCoordinator alloc]initWithManagedObjectModel:model];
//創(chuàng)建文件路徑
NSString *filePath =[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject stringByAppendingPathComponent:@"model.sqlite"];
//關(guān)聯(lián)路徑
//如果需要版本迭代的話,那么要在options設(shè)置@{NSMigratePersistentStoresAutomaticallyOption:@YES,NSInferMappingModelAutomaticallyOption:@YES}并且要在一開始的時候就需要設(shè)置
[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)建一個學(xué)生
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(@"集合的內(nèi)容%@",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. 設(shè)置查詢條件(這一步可有可無,根據(jù)需求來決定)
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. 設(shè)置查詢條件(這一步可有可無,根據(jù)需求來決定)
2.1 查詢:經(jīng)常用到的有三種,第一種直接查詢.(name=張三)
第二種模糊查? NSPredicate *predicate =[NSPredicate predicateWithFormat:@"name CONTAINS %@",@"張"];
第三種:以什么結(jié)尾或以聲明開頭: NSPredicate *pre =[NSPredicate predicateWithFormat:@"name LIKE %@",@"*棟"];
用請求體點出條件.
3. 用context去執(zhí)行查詢請求 并用數(shù)組接受
*/
//nsfetchrequest
//創(chuàng)建查詢學(xué)生表的請求體
//? ? NSFetchRequest *request =[NSFetchRequest fetchRequestWithEntityName:@"Student"];
//? ? //(第一種)查詢年齡為20歲的人
//? ? /** 謂詞*/
//? ? NSPredicate *predicate =[NSPredicate predicateWithFormat:@"age =%@",@20];
//? ? //加入謂詞條件
//? ? request.predicate =predicate;
//****************************
//(第二種)查詢姓張的人
//? ? NSPredicate *predicate =[NSPredicate predicateWithFormat:@"name CONTAINS %@",@"張"];
//? ? request.predicate=predicate;
//(第三種)查詢名子以棟結(jié)尾的人
//? ? 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(@"%@的學(xué)生 %@ %@ %@",tea.name,stu.name,stu.age,stu.number);
}
}
}
@end