关闭

iOSCoreData数据库:

标签: 数据库
133人阅读 评论(0) 收藏 举报
分类:
@property (readonly, strong, nonatomic) NSManagedObjectContext *managedObjectContext;//操作管理器(增删改查)
@property (readonly, strong, nonatomic) NSManagedObjectModel *managedObjectModel;//被管理的数据模型
@property (readonly, strong, nonatomic) NSPersistentStoreCoordinator *persistentStoreCoordinator;//持久化协助管理器

  /**
     * 表示要向Person这个表里面插入一条数据
     */
    /*
     参数1:插入的表名
     参数2:托管上下文对象(coreData操作管理器)

     */
    //数据  ->(映射) 对象
    Person *per = [NSEntityDescription insertNewObjectForEntityForName:@"Person" inManagedObjectContext:self.managedObjectContext];
    //设置返回实体对象的属性
    per.name = @"老王";
    per.age = [NSNumber numberWithInt:11];
    per.headImage = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"1" ofType:@"png"]];

    //保存记录
    [self.managedObjectContext save:nil];

    NSLog(@"%@",NSHomeDirectory());


    //查找数据
    NSFetchRequest *request = [[NSFetchRequest alloc] init];

    //获得表结构实体(把一张表进行实体化)
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Person" inManagedObjectContext:self.managedObjectContext];

    //设置查询的表结构实体(设置查询的是哪张表)
    request.entity = entity;



    /*
    //设置条件查询(谓语)
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name = %@",@"老李"];

    request.predicate = predicate;
     */

    //设置升降序排列
    //YES代表升序,NO代表降序
    NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"age" ascending:YES];

    request.sortDescriptors = @[sort];

    //通过操作管理器发起查询请求
    //resultArr储存搜寻的结果
    NSArray *resultArr = [self.managedObjectContext executeFetchRequest:request error:nil];

    for (Person *per in resultArr) {

        NSLog(@"per.name = %@ per.age = %@",per.name,per.age);

    }


    /*
    for (Person *per in resultArr) {

        NSLog(@"per.name = %@ per.age = %@",per.name,per.age);
        //修改数据(更新)
        per.name = @"老李";

        //保存记录
        [self.managedObjectContext save:nil];

        //删除
        if ([per.age integerValue] == 90) {

            [self.managedObjectContext deleteObject:per];

            //保存记录
            [self.managedObjectContext save:nil];

        }

    }
     */

.M中:
    NSManagedObjectContext *_context;
引入头文件#import "AppDelegate.h"
- (void)loadDataFromCoreData
{
    //coreData查询

    AppDelegate *delegate = [UIApplication sharedApplication].delegate;

    _context = delegate.managedObjectContext;

    NSFetchRequest *request = [[NSFetchRequest alloc]init];

    //从哪个实体中查询数据
    NSEntityDescription *descrip = [NSEntityDescription entityForName:@"CDGoodsModel" inManagedObjectContext:_context];
    //查询规则,where xx = xx
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"cid = %d",_cid];
    [request setEntity:descrip];
    [request setPredicate:predicate];

    NSArray *array = [_context executeFetchRequest:request error:nil];
    [_dataArray addObjectsFromArray:array];
    [_myTable reloadData];
}

数据库删除:
AppDelegate *delegate = [UIApplication sharedApplication].delegate;
        _context = delegate.managedObjectContext;

        for (NSManagedObject *obj in _dataArray)
        {
            [_context deleteObject:obj];
        }
        [_context save:nil];

        [_dataArray removeAllObjects];


数据库插入:CDGoodsModel:表名
   CDGoodsModel *cdModel = [NSEntityDescription insertNewObjectForEntityForName:@"CDGoodsModel" inManagedObjectContext:_context];
            cdModel.imgURL = [NSString stringWithFormat:@"%@",model.imgURL];
            cdModel.price = [NSString stringWithFormat:@"%@",model.price];
            cdModel.discount = [NSString stringWithFormat:@"%@",model.discount];
            cdModel.cid = [NSString stringWithFormat:@"%d",_cid];

            NSError *error = nil;
            if(![_context save:&error])
            {
                NSLog(@"coreData保存数据失败,error=%@",error);
            }else
            {
                [_dataArray addObject:cdModel];
            }

        }

        //刷表
        [_myTable reloadData];

URL 的赋值不可以放到init里

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:6370次
    • 积分:192
    • 等级:
    • 排名:千里之外
    • 原创:13篇
    • 转载:2篇
    • 译文:0篇
    • 评论:0条
    文章分类
    文章存档