coredata

作者: 艾克12138 | 来源:发表于2016-06-25 20:58 被阅读42次

    #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];

    //创建文件路径

    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 {

    //创建一个学生

    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.创建查询提

    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.创建查询提

    2. 设置查询条件(这一步可有可无,根据需求来决定)

    2.1 查询:经常用到的有三种,第一种直接查询.(name=张三)

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

    第三种:以什么结尾或以声明开头: NSPredicate *pre =[NSPredicate predicateWithFormat:@"name LIKE %@",@"*栋"];

    用请求体点出条件.

    3. 用context去执行查询请求 并用数组接受

    */

    //nsfetchrequest

    //创建查询学生表的请求体

    //    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];

    //执行查询操作

    //  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

    相关文章

      网友评论

          本文标题:coredata

          本文链接:https://www.haomeiwen.com/subject/hwaodttx.html