CoreData基本使用-02-

//
//  ViewController.m
//  01-CoreData基本使用
//
//  Created by mac on 16/5/4.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "ViewController.h"
#import <CoreData/CoreData.h>
#import "User.h"

@interface ViewController ()

@end

@implementation ViewController {

    NSManagedObjectContext *_managerObjectContext;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    [self openDataBase];
//    [self addUser];
//    [self searchUserWithUserID:55];
    [self updateUser];
    [self deleteUser];

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

/**
 *  1. 打开数据库
 */
- (void)openDataBase {

    //1. 创建数据库文件
    NSString *dataBaseFilePath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/User.sqlite"];
    NSURL *dataBaseFileUrl = [NSURL fileURLWithPath:dataBaseFilePath];

    //2. 创建模型描述文件
    /** a. 创建模型描述文件 b. */

    //3. 获取模型描述文件路径
    NSURL *entityFileURL = [[NSBundle mainBundle] URLForResource:@"UserModel" withExtension:@"momd"];
    NSLog(@"%@", entityFileURL);

    //4. 通过模型描述文件 来创建pcs(持久化存储坐标)

    NSManagedObjectModel *model = [[NSManagedObjectModel alloc] initWithContentsOfURL:entityFileURL];// model
    NSPersistentStoreCoordinator *pcs = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:model];

    //5.创建实体数据库文件
    NSError *error = nil;
    [pcs addPersistentStoreWithType:NSSQLiteStoreType configuration:NULL URL:dataBaseFileUrl options:nil error:&error];
    if (error) {
        NSLog(@"失败");
    } else {

        NSLog(@"成功");
    }

    _managerObjectContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];
    _managerObjectContext.persistentStoreCoordinator = pcs;
}
/**
 *  2. 添加数据
 */
- (void)addUser {

  /*  //1. 创建MO对象并且添加到objectContext
//    User *user1 = [[User alloc] init];
    User *user1 = (User *)[NSEntityDescription insertNewObjectForEntityForName:@"User" inManagedObjectContext:_managerObjectContext];

    //2. 使用context 将数据存储到数据库
    user1.username = @"zhangsan";
    user1.password = @"1234";
    user1.user_id = @1001;

    //3. 使用context将数据存储到数据库
    NSError *error = nil;
    [_managerObjectContext save:&error];
    if (error) {
        NSLog(@"保存失败%@", error);
    } else {

        NSLog(@"保存成功");
    } */

    for (int i=0; i<100; i++) {

        User *user = [NSEntityDescription insertNewObjectForEntityForName:@"User" inManagedObjectContext:_managerObjectContext];

        user.username = [NSString stringWithFormat:@"user%i", i];
        user.user_id = @(i);
        user.password = @"123456";

        //使用context将数据存储到数据库
        NSError *error = nil;
        [_managerObjectContext save:&error];
        if (error) {
            NSLog(@"保存失败");
        } else {
            NSLog(@"保存成功");
        }
    }

}
/**
 *  3. 查询数据
 */
- (void)searchUserWithUserID:(NSInteger)userID {

    //1. 构建查询请求
    NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"User"];

    //2. 设置查询的条件
    //3. 使用谓词来设定查询的条件
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"user_id>%li",userID];
    fetchRequest.predicate = predicate;

    //4. 执行查询请求
    NSError *error = nil;
    NSArray *resultArray = [_managerObjectContext executeFetchRequest:fetchRequest error:&error];

    //5. 处理查询结果
    if (error) {
        NSLog(@"查询出错%@", error);
    } else {

        //查询成功
        for (User *user in resultArray)
            NSLog(@"name = %@, password = %@, user_id = %@", user.username, user.password, user.user_id);
        }
}
/**
 *  4. 修改数据
 */
- (void)updateUser {

    //0. 将密码修改为88888888

    //1. 从数据库中查到需要修改的数据
    NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"User"];
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"user_id=88"];
    request.predicate = predicate;

    //2. 执行查询请求
    NSArray *array = [_managerObjectContext executeFetchRequest:request error:nil];

    //3. 修改这些数据
    for (User *user in array) {
        user.password = @"88888888";
    }

    //4. 将修改后的数据保存到数据库
    [_managerObjectContext save:nil];
}
/**
 *  5. 删除数据
 */
- (void)deleteUser {

    //1. 查询需要删除的用户对象
    NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"User"];
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"user_id=86"];
    request.predicate = predicate;

    //2. 执行查询的请求
    NSArray *array = [_managerObjectContext executeFetchRequest:request error:nil];

    //3. 修改数据
    for (User *user in array) {
        [_managerObjectContext deleteObject:user];
    }

    //4. 将修改后的数据保存到数据库
    [_managerObjectContext save:nil];

}

@end
时间: 2024-10-24 13:57:33

CoreData基本使用-02-的相关文章

百度刚放假啊数据库风口浪尖萨拉疯了

http://www.ebay.com/cln/l_x5585/2015.02.11/176746639012 http://www.ebay.com/cln/jiacha_boryk/2015.02.11/176837188016 http://www.ebay.com/cln/gbnlin0/2015.02.11/176837189016 http://www.ebay.com/cln/j_j2841/2015.02.11/177066749015 http://www.ebay.com/c

百度房间爱师傅卡卡是快乐疯了;爱死

http://www.ebay.com/cln/shx9479/-/177007606013/2015.02.11 http://www.ebay.com/cln/genqi12/-/176846034010/2015.02.11 http://www.ebay.com/cln/seyyon2/-/176906811016/2015.02.11 http://www.ebay.com/cln/wcn5971/-/176846032010/2015.02.11 http://www.ebay.co

百度和房价是否健康教案上开发

http://www.ebay.com/cln/l.kuan2/-/167247714018/2015.02.10 http://www.ebay.com/cln/setlia-3616/-/167086016019/2015.02.10 http://www.ebay.com/cln/pen-y77/-/167086017019/2015.02.10 http://www.ebay.com/cln/yua-me2/-/167399441016/2015.02.10 http://www.eba

百度电话费健身房拉伸件礼服加拉斯减肥

http://www.ebay.com/cln/cnli_c90nphs5e/-/167379958016/2015.02.07 http://www.ebay.com/cln/gaw4612/-/167226239018/2015.02.07 http://www.ebay.com/cln/re_len4/-/167263594010/2015.02.07 http://www.ebay.com/cln/ta.ku83/-/167162702017/2015.02.07 http://www.

百度回复金卡是减肥拉进来收付款

http://www.ebay.com/cln/cnli_c90nphs5e/-/167379958016/2015.02.08 http://www.ebay.com/cln/gaw4612/-/167226239018/2015.02.08 http://www.ebay.com/cln/re_len4/-/167263594010/2015.02.08 http://www.ebay.com/cln/ta.ku83/-/167162702017/2015.02.08 http://www.

百度放假哈萨克就发了设计费拉萨

http://www.ebay.com/cln/ldicn.mz6dm/2015.02.11/177030163015 http://www.ebay.com/cln/tan_qi5/2015.02.11/176903144013 http://www.ebay.com/cln/l.lu104/2015.02.11/177030175015 http://www.ebay.com/cln/ya01191/2015.02.11/176722580014 http://www.ebay.com/cl

百度房间撒谎发卡上就发了空间啊

http://www.ebay.com/cln/h-h4129/2015.02.11/176819191016 http://www.ebay.com/cln/fendo88/2015.02.11/176613943017 http://www.ebay.com/cln/ygon288/2015.02.11/176727517018 http://www.ebay.com/cln/ta.ch17/2015.02.11/176613950017 http://www.ebay.com/cln/g-

百度房间沙发客服就考试考几分离开

http://www.ebay.com/cln/jinlon8/book/167309734010/2015.02.10 http://www.ebay.com/cln/bam5330/book/167115292019/2015.02.10 http://www.ebay.com/cln/yi_za70/book/167315676012/2015.02.10 http://www.ebay.com/cln/y.y3463/book/167285977014/2015.02.10 http:/

Web Service学习笔记之----JAX-RPC

众所周知,数据科学是这几年才火起来的概念,而应运而生的数据科学家(data scientist)明显缺乏清晰的录取标准和工作内容.此次课程以<星际争霸II>回放文件分析为例,集中在IBM Cloud相关数据分析服务的应用.面对星际游戏爱好者希望提升技能的要求,我们使用IBM Data Science Experience中的jJupyter Notebooks来实现数据的可视化以及对数据进行深度分析,并最终存储到IBM Cloudant中.这是个介绍+动手实践的教程,参会者不仅将和讲师一起在线