CoreData总结-增删改查操作

#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 = [ self getManagedContext ];
    
     // 获得 Student 数据表一行数据对应的模型   
    NSEntityDescription * stu =[ NSEntityDescription entityForName : @"Student" inManagedObjectContext :context];
    
    // 实例化 NSFetchRequest 对象,相当于实例化一条 SQL 语句对象
    NSFetchRequest * request  = [[NSFetchRequest alloc ] init ];
    
    // 设置 SQL 语句将要操作的数据模型
    [request
setEntity :stu];
    
    // 构造查询条件,相当于 where 字句
    NSPredicate * predicate = [ NSPredicate predicateWithFormat : @"s_id=%i" , 1 ];
    
   
// 将查询条件添加到 predicate 中,相当于 sql 语句中
    [request
setPredicate :predicate];
    
   
NSArray * students = [context executeFetchRequest :request error : nil ];
    
   
if (students. count > 0 )
    {
       
NSManagedObject * obj =[students lastObject ];
        [obj
setValue : @"c#" forKey : @"s_name" ];
    }
    [context
save : nil ];
    [
self select ];
}

你可能感兴趣的:(CoreData总结-增删改查操作)