CoreData总结-增删改查操作

本文介绍了一个使用Objective-C实现的Core Data基本操作示例,包括如何创建、读取、更新和删除数据对象。通过具体的代码示例,展示了如何进行实体的插入、查询、更新及删除操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#import <CoreData/CoreData.h>
#import
"AppDelegate.h"
#import
"Classes.h"
#import
"Student.h"
- (void)insert
{
   
//获得AppDelegate对象得到NSManagedObjectContext的对象
   
//    AppDelegate *delegate = [[UIApplication sharedApplication] delegate];
   
//    NSManagedObjectContext *context = [delegate managedObjectContext];
   
NSManagedObjectContext *context = [self getManagedContext];
   
//获得管理对象来管理实体Student,找到实体结构,生成实体对象insertNewObjectForEntityForName方法,区分大小写
   
NSManagedObject *stu = [NSEntityDescription insertNewObjectForEntityForName:@"Student" inManagedObjectContext:context];
   
//班级实体
   
//    NSManagedObject *class1 = [NSEntityDescription insertNewObjectForEntityForName:@"Classes" inManagedObjectContext:context];
   
//    [class1 setValue:[NSNumber numberWithInt:1] forKey:@"c_id"];
   
//    [class1 setValue:@"classOne" forKey:@"c_name"];
   
   
Classes *class1 = [NSEntityDescription insertNewObjectForEntityForName:@"Classes" inManagedObjectContext:context];
    class1.
c_id = [NSNumber numberWithInt:2];
    class1.
c_name = @"classTwo";
   
   
//给实体对象赋值
    [stu
setValue:[NSNumber numberWithInt:1] forKey:@"s_id"];
    [stu
setValue:@"java" forKey:@"s_name"];

    [stu
setValue:class1 forKey:@"Classes"];
   
   
Student *stu1 = [NSEntityDescription insertNewObjectForEntityForName:@"Student" inManagedObjectContext:nil];
    stu1.
s_id = [NSNumber numberWithInt:2];
    stu1.
s_name = @"c++";
   
   
//调用Context,保存实体
   
NSError *error;
   
if([context save:&error]){
       
NSLog(@"OK java in");
    }
   
else{
       
NSLog(@"%@",error);
    }
}

- (
NSManagedObjectContext *)getManagedContext
{
   
AppDelegate *delegate = [[UIApplication sharedApplication]delegate];
   
NSManagedObjectContext *context = [delegate managedObjectContext];
   
return context;
}

- (
void)select
{
   
NSManagedObjectContext *context;
    context = [
self getManagedContext];
   
   
//查找对象entityForName方法
   
NSEntityDescription *stu = [NSEntityDescription entityForName:@"Student" inManagedObjectContext:context];
   
   
//构造一个查询对象(查询对象)
   
NSFetchRequest *request = [[NSFetchRequest alloc]init];
   
   
//设置
    [request
setEntity:stu];
   
   
//执行查询,返回结果集
   
NSArray *resultArray = [context executeFetchRequest:request error:nil];
   
   
//遍历结果集
   
for(NSManagedObject *entity in resultArray){
       
NSLog(@"id = %i,name = %@,class = %@",[[entity valueForKey:@"s_id"] intValue],[entity valueForKey:@"s_name"],[[entity valueForKey:@"classes"] valueForKey:@"c_name"]);
    }
}

- (
void)update
{
   
//    [self insert];
   
//    [self select];
   
   
NSManagedObjectContext *context = [self getManagedContext];
   
NSEntityDescription *stu = [NSEntityDescription  entityForName:@"Student" inManagedObjectContext:context];
   
NSFetchRequest *request = [[NSFetchRequest alloc]init];
    [request
setEntity:stu];
   
   
//构造查询条件,相当于where语句
   
NSPredicate *perdicate = [NSPredicate predicateWithFormat:@"s_id=%d",1];
    [request
setPredicate:perdicate];
   
   
//*代表任意个字符,含有le的字符
   
NSPredicate *p1 = [NSPredicate predicateWithFormat:@"s_name like '*%@'",@"le"];
   
//?代表一个字符,第一个字符随意,紧着着含有le
   
NSPredicate *p2 = [NSPredicate predicateWithFormat:@"s_name like '%@'",@"le"];
   
//查询1-10
   
NSPredicate *p3 = [NSPredicate predicateWithFormat:@"s_name between{1,10}"];
   
//classes.c_name = 'classOne'
   
//s_name beginWith 'yang' = =s_name like 'yang*'(开头结尾和包含)ednWith,contains
   
   
//执行查询
   
NSArray *students = [context executeFetchRequest:request error:nil];
   
if (students.count>0) {
       
NSManagedObject *obj = [students lastObject];
        [obj
setValue:@"ios" forKey:@"s_name"];
       
    }
    [context
save:nil];
    [
self select];
   
}
- (
void)delete
{
   
NSManagedObjectContext *context = [self getManagedContext];
   
NSEntityDescription *stu = [NSEntityDescription  entityForName:@"Student" inManagedObjectContext:context];
   
NSFetchRequest *request = [[NSFetchRequest alloc]init];
    [request
setEntity:stu];
   
   
//构造查询条件,相当于where语句
   
NSPredicate *perdicate = [NSPredicate predicateWithFormat:@"s_id=%d",1];
    [request
setPredicate:perdicate];
   
   
//执行查询
   
NSArray *students = [context executeFetchRequest:request error:nil];
   
if (students.count>0) {
       
        [context
deleteObject:[students lastObject]];
        [context
save:nil];
    }
}

- (void)upDate {
    NSManagedObjectContext * context = [selfgetManagedContext];
    
     //获得Student数据表一行数据对应的模型  
    NSEntityDescription * stu =[ NSEntityDescriptionentityForName:@"Student"inManagedObjectContext:context];
    
    //实例化NSFetchRequest对象,相当于实例化一条SQL语句对象
    NSFetchRequest * request  = [[NSFetchRequestalloc]init];
    
    //设置SQL语句将要操作的数据模型
    [request
setEntity:stu];
    
    //构造查询条件,相当于where字句
    NSPredicate * predicate = [NSPredicatepredicateWithFormat:@"s_id=%i",1];
    
   
//将查询条件添加到predicate中,相当于sql语句中
    [request
setPredicate:predicate];
    
   
NSArray* students = [contextexecuteFetchRequest:requesterror:nil];
    
   
if(students.count>0)
    {
       
NSManagedObject * obj =[studentslastObject];
        [obj
setValue:@"c#"forKey:@"s_name"];
    }
    [context
save:nil];
    [
selfselect];
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值