(1) 什么是CoreData 提供对象-关系映射 (ORM) 将OC对象转化为数据,保存到SQLite数据库文件中 不需要编写任何SQL语句
Employee * emp = [[Employee alloc] init];
emp.name = @”xxx”;
emo.age = 12;
平时写入数据库
insert into employee (name,age) value (xxx,12) //即可省略这一步 不用和SQL语句打交道
CoreData的使用步骤
1.创建模型文件
2.添加实体 一个实体对应一个数据库表
3.创建实体类 描述字段
4.生成上下文 关联模型文件生成数据库
5.保存对象到数据库
6.从数据库获取对象
7.更新数据
8.删除数据
coreData的增删改查
CoreData在公司用的比较少,用户用的比较多的是FMDatabases
数据存储的结构比较简单的时候,使用CoreData 开发效率会很高 直接面向对象,而且不用写sql语句
FMDatabases 数据结果比较复杂的时候,表与表之间的关联比较多的时候
//
// ViewController.m
// coredata简单使用 纯代码 不使用系统生成的
//
// Created by EMPty on 3/11/16.
// Copyright (c) 2016 EMPty. All rights reserved.
//
#import "ViewController.h"
#import <CoreData/CoreData.h>//导入头文件
#import "Employee.h"
@interface ViewController ()
{
NSManagedObjectContext * _context;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// 1.创建模型文件 【相当于一个数据库里的表】
//新建一个coredata分类里的model
// 2.添加实体【一张表】
//add Entity 一个实体(Entity)就是一张表
// 3.创建实体类【相当于模型】
//新建 NSManagedObject subclass
//float 变成 NSNumber 基本类型也变成OC对象
// 4.生成上下文 关联模型文件生成数据库
/*
关联的时候,如果本地没有数据库文件,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* sqliteoath = [doc stringByAppendingPathComponent:@"Company.sqlite"];
NSLog(@"%@",sqliteoath);
[store addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:[NSURL fileURLWithPath:sqliteoath] options:nil error:nil];
context.persistentStoreCoordinator = store;
_context = context;
}
#pragma mark - 数据库的操作 CURD Creat Upload Read Delete
#pragma mark 添加员工
- (void) addEmployee//调用这个方法就能添加数据到数据库里面
{
//创建一个员工对象
// Employee* emp = [[Employee alloc]init];
Employee* emp = [NSEntityDescription insertNewObjectForEntityForName:@"Employee" inManagedObjectContext:_context];
emp.name = @"xiaohuang ";
// emp.height = [NSNumber numberWithFloat:190];
emp.height = @1.90;
emp.birthday = [NSDate date];
//直接保存到数据库
NSError * error = nil;
[_context save:&error];
if (error) {
NSLog(@"%@",error);
}
}
#pragma mark - 读取
- (void) readEmployee
{
//Entity Name 表的名字
//Predicate 过滤
//Sort Prderings 排序
//1.FetchRequest 抓取请求对象
NSFetchRequest* request = [NSFetchRequest fetchRequestWithEntityName:@"Employee"];//表名
//2.设置过滤条件
//查找
NSPredicate * pre = [NSPredicate predicateWithFormat:@"name = %@",@"xiaohuang"];
request.predicate = pre;
//3.设置排序
//身高的升序排序
NSSortDescriptor* heightSort = [NSSortDescriptor sortDescriptorWithKey:@"height" ascending:YES];//是不是升序
request.sortDescriptors = @[heightSort];
//4.执行请求
NSError* error = nil;
NSArray* emps = [_context executeFetchRequest:request error:&error];
if (error) {
NSLog(@"%@",error);
}
NSLog(@"employee: %@",emps);
for (Employee* tem in emps) {
NSLog(@"名字:%@ 身高:%@ 生日:%@",tem.name,tem.height,tem.birthday);
}
}
#pragma mark - 更新员工
- (void)updataEmployee
{
//改xiaohuang的身高
//1.查找到xiaohuang
NSFetchRequest* request = [NSFetchRequest fetchRequestWithEntityName:@"Employee"];
NSPredicate* pre = [NSPredicate predicateWithFormat:@"name = %@",@"xiaohuang"];
request.predicate = pre;
//1.2执行请求
NSArray* emps = [_context executeFetchRequest:request error:nil];
NSLog(@"%@",emps);
//2.更改身高
for (Employee* emp in emps) {
emp.height = @2.0;
}
//3.保存
[_context save:nil];
}
#pragma mark - 删除
- (void)deleteEmployee
{
//删除xiaohaung
//1.查找xiaohuang
NSFetchRequest* request = [NSFetchRequest fetchRequestWithEntityName:@"Employee"];
NSPredicate* pre = [NSPredicate predicateWithFormat:@"name = %@",@"xiaohuang"];
request.predicate = pre;
//1.2执行请求
NSArray* emps = [_context executeFetchRequest:request error:nil];
NSLog(@"%@",emps);
//2.删除
for (Employee* temp in emps) {
[_context deleteObject:temp];
}
//3.保存
[_context save:nil];
}
#pragma mark - 分页查询
- (void)fenyeSearchEmployee
{
//分页查询 limit 0,5 sql语句
NSFetchRequest* request = [NSFetchRequest fetchRequestWithEntityName:@"Employee"];
//分页的初始索引
request.fetchOffset = 0;
//分页的条数 每一页的数目
request.fetchLimit = 6;
//每页6个 0开始的6个
// request.fetchOffset = 6;
//每页6个 6开始的6个
//身高升序
NSSortDescriptor* des = [NSSortDescriptor sortDescriptorWithKey:@"height" ascending:YES];
request.sortDescriptors = @[des]; //这是个数组
//执行请求
NSError* error = nil;
NSArray* emps = [_context executeFetchRequest:request error:&error];
if (error) {
NSLog(@"%@",error);
}
NSLog(@"employee: %@",emps);
for (Employee* tem in emps) {
NSLog(@"名字:%@ 身高:%@ 生日:%@",tem.name,tem.height,tem.birthday);
}
}
#pragma mark - 模糊查询
- (void)fuzzySearch
{
NSFetchRequest* request = [NSFetchRequest fetchRequestWithEntityName:@"Employee"];
//名字以xiao开头
//过滤条件
NSPredicate* pre = [NSPredicate predicateWithFormat:@"name BEGINSWITH %@",@"xiao"];
request.predicate = pre;
//名字以ang结尾
NSPredicate* pre1 = [NSPredicate predicateWithFormat:@"name ENDSWITH %@",@"ang"];
//名字包含a
NSPredicate* pre2 = [NSPredicate predicateWithFormat:@"name CONTAINS %@",@"a"];//包含 没有WITH
//like *ang 以ang结尾
NSPredicate* pre3 = [NSPredicate predicateWithFormat:@"name like %@",@"xiao*"];//*通配符 以xiao开头
//执行请求
NSError* error = nil;
NSArray* emps = [_context executeFetchRequest:request error:&error];
if (error) {
NSLog(@"%@",error);
}
NSLog(@"employee: %@",emps);
for (Employee* tem in emps) {
NSLog(@"名字:%@ 身高:%@ 生日:%@",tem.name,tem.height,tem.birthday);
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end