iOS首页投稿(暂停使用,暂停投稿)iOS 开发

数据库操作语句

2016-06-06  本文已影响628人  码路芽子

CoreData

CoreData的使用步骤

- 创建模型文件
- 添加实体
- 创建实体类
- 生成上下文件 关联模型文件生成数据库
- 保存对象到数据库
- 从数据库获取对象
- 更新数据
- 删除数据

数据持久化:是将一个数据保存到文件中,而不是内存中

CoreData简单用法

/**
 1. 创建模型文件
 2. 添加实体
 3. 创建实体类(就是一张表)
 4. 生成上下文件 关联模型文件生成数据库
 */

- (void)setUpCoreData {
   /** 关联的时候:如果本地没有这个文件,coreData会自己创建 */
  /** 生成上下文 */
  NSManagedObjectContext *context = [[NSManagedObjectContext alloc] init];
  /** 模型文件 */
  NSManagedObjectModel *model = [NSManagedObjectModel mergedModelFromBundles:nil];
  /** 上下文要关联数据库 */
  /** 持久化调度器 */
  NSPersistentStoreCoordinator *store = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:model];
  /** 告诉CoreData数据库的名字和路径 */
  NSString *doc = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
  NSString *sqlitePath = [doc stringByAppendingString:@"company.sqlite"];

  [store addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:[NSURL fileURLWithPath:sqlitePath] options:nil error:nil];

  context.persistentStoreCoordinator = store;

  _context = context;
}


#pragma mark - 添加
- (IBAction)addEntity {
    
    Entity *enti = [NSEntityDescription insertNewObjectForEntityForName:@"Entity" inManagedObjectContext:_context];
    enti.name = @"wangwu";
    enti.hight = @2.30;
    
    NSError *error = nil;
    [_context save:&error];
    
    if (error) {
        NSLog(@"%@", error);
    }
}

#pragma mark - 读取员工
- (IBAction)readEntity {
    
    /** 1. FetchRequest抓取对象 */
    NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Entity"];
    
    /** 2. 设置过滤条件 */
    NSPredicate *predic = [NSPredicate predicateWithFormat:@"name = %@", @"wangwu"];
    request.predicate = predic;
    
    /** 3. 排序 */
    /** 身高的升序排序 */
    NSSortDescriptor *heightSort = [NSSortDescriptor sortDescriptorWithKey:@"hight" ascending:YES];
    request.sortDescriptors = @[heightSort];/*< 数组的条件排序 */
    
    
    /** 4. 执行请求 */
    NSError *error = nil;
    NSArray *arr = [_context executeFetchRequest:request error:&error];
    if (error) {
        NSLog(@"%@", error);
    }
    
    /** 遍历数组,读取的内容 */
    for (Entity *ti in arr) {
        NSLog(@"%@, %@", ti.name, ti.hight);
    }
}

#pragma mark - 更新员工

- (IBAction)updateEntity {
    
    /** 更改张三  身高两米 */
    
    /** 1.查找张三 */
    NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Entity"];
    
    /** 设置过滤条件 */
    NSPredicate *predic = [NSPredicate predicateWithFormat:@"name = %@", @"zhangsan"];
    request.predicate = predic;
    /** 执行请求 */
    NSArray *enti = [_context executeFetchRequest:request error:nil];
    
    /** 2.更新身高 */
    for (Entity *en in enti) {
        en.hight = @2.0;
    }
    
    /** 3.保存 */
    [_context save:nil];
}

#pragma mark - 删除
- (IBAction)delete {
    
    /** 找到list */
    /** .查找张三 */
    NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Entity"];
    
    NSPredicate *predic = [NSPredicate predicateWithFormat:@"name = %@", @"zhangsan"];
    request.predicate = predic;
    
    NSArray *enti = [_context executeFetchRequest:request error:nil];
    
    /** 删除 */
    for (Entity *entity in enti) {
        [_context deleteObject:entity];
    }
    
    /** 保存 */
    [_context save:nil];
}

数据库软件使用中的语句

上一篇 下一篇

猜你喜欢

热点阅读