iOS我的ios进阶数据库

ios - coredata增删改查简单使用

2016-05-08  本文已影响2209人  AlexPei
Paste_Image.png Paste_Image.png Paste_Image.png Paste_Image.png Paste_Image.png Paste_Image.png Paste_Image.png Paste_Image.png

//_managedObjectContext这是管理数据库中所有实体的上下文,也就是通过他可以查找数据库中的数据
@synthesize managedObjectContext = _managedObjectContext;
//对象模型,根据对象模型来存储数据
@synthesize managedObjectModel = _managedObjectModel;
//持久化数据存储调度器,也就是通过它可以把数据存到数据库中
@synthesize persistentStoreCoordinator = _persistentStoreCoordinator;

Paste_Image.png

/** 保存 增加数据 */
- (IBAction)saveClick:(id)sender {
    
//获取AppDelegate中的被管理所有对象的上下文managedObjectContext
    AppDelegate * delegate = [[UIApplication sharedApplication] delegate];
    NSManagedObjectContext * context = delegate.managedObjectContext;
//创建对象,来分别保存输入的内容
    Person * per = [NSEntityDescription insertNewObjectForEntityForName:@"Person" inManagedObjectContext:context];
    //获取输入的各项内容
    per.name = self.name.text;
    per.age = self.age.text;
    per.phone = self.phone.text;
    //创建sex对象,把sex对象赋值给Perosn中的sex属性,同时保存sex
    Sex * sex = [NSEntityDescription insertNewObjectForEntityForName:@"Sex" inManagedObjectContext:context];
    sex.sex = self.sex.text;
    per.sex = sex;
//保存数据
    NSLog(@"保存");
    [delegate saveContext];
}


/** 查询 */
- (IBAction)search:(id)sender {
    
    NSString * searchContent = self.searchTxt.text;
    NSPredicate * pre = [NSPredicate predicateWithFormat:@"name CONTAINS %@",searchContent];
    /**
     1.谓词查询内容 
     2.如果输入要查询的内容长度为0,则显示保存的所有的数据
     */
    if (searchContent.length == 0) {
        pre = nil;
    }
    
    AppDelegate * delegate = [[UIApplication sharedApplication] delegate];
    
    NSManagedObjectContext * context = delegate.managedObjectContext;
    
    NSEntityDescription * des = [NSEntityDescription entityForName:@"Person" inManagedObjectContext:context];
    
    NSFetchRequest * request = [NSFetchRequest new];
    request.entity = des;
    request.predicate = pre;
    
    /** 获取查询到的内容---是数组 */
    NSArray * array = [context executeFetchRequest:request error:NULL];
    //遍历查询到的数据,并打印
    for (Person * p in array) {
        NSLog(@" %@ : %@ : %@ : %@",p.name,p.age,p.sex.sex,p.phone);
    }
}


/** 删除 */
- (IBAction)delete:(id)sender {
    
    /** 
     1.查询到要删除的内容 
     2.获取输入的内容
     */
    NSString * deleteContent = self.searchTxt.text;
    
    /** 查询要删除带有输入的关键字的对象 */
    NSPredicate * pre = [NSPredicate predicateWithFormat:@"name CONTAINS %@",deleteContent];
    
    AppDelegate * delegate = [[UIApplication sharedApplication] delegate];
    
    /** 被管理对象上下文--(获取所有被管理对象的实体) */
    NSManagedObjectContext * context = delegate.managedObjectContext;
    
    /** 根据上下文获取查询数据库实体的请求参数---要查询的entity(实体) */
    NSEntityDescription * des = [NSEntityDescription entityForName:@"Person" inManagedObjectContext:context];
    
    /** 查询请求 */
    NSFetchRequest * request = [NSFetchRequest new];
    
    /** 根据参数获取查询内容 */
    request.entity = des;
    
    request.predicate = pre;
    
    /** 
     1.获取所有被管理对象的实体---根据查询请求取出实体内容 
     2.获取的查询内容是数组
     3.删掉所有查询到的内容
     3.1.这里是模糊查询 即 删除包含要查询内容的字母的内容
     */
    NSArray * array = [context executeFetchRequest:request error:NULL];
    
    /** 对查询的内容进行操作 */
    for (Person * p in array) {
        [context deleteObject:p];
    }
    NSLog(@"删除完成");
    [delegate saveContext];
}



/** 修改 */
- (IBAction)update:(id)sender {
    
    /** 获取输入内容 */
    NSString * updateContent = self.searchTxt.text;
    NSPredicate * pre = [NSPredicate predicateWithFormat:@"name CONTAINS %@",updateContent];
    
    AppDelegate * delegate = [UIApplication sharedApplication].delegate;
    NSManagedObjectContext * context = delegate.managedObjectContext;
    NSEntityDescription * des = [NSEntityDescription entityForName:@"Person" inManagedObjectContext:context];
    NSFetchRequest * request = [NSFetchRequest new];
    request.entity = des;
    request.predicate = pre;
    
    NSArray * array = [context executeFetchRequest:request error:NULL];
    
//这里修改的话把全部查询到的内容修改成了 "张三",可以根据自己的需要进行设置
    for (Person *p in array) {
        p.name = @"张三";
        [context updatedObjects];
    }
    NSLog(@"修改完成");
    [delegate saveContext];
}

谓词使用小结:

Paste_Image.png
Paste_Image.png Paste_Image.png

简单的增删改查完成,感兴趣的可以做一下tableView的cell,对cell的数据进行增删改查,设置代理,实时监听数据库数据的变化,来实时刷新tableView即可

上一篇 下一篇

猜你喜欢

热点阅读