文章目录
  1. 1. CoreData
    1. 1.1. 使用过程
    2. 1.2. 表连接

CoreData

  • CoreData是对sqlite的一次封装。

使用过程

  • 首先新建一个 Data Model
    • 添加数据模型(相当于数据库)
    • 添加实体对象(相当于一张表)
    • 添加实体对象类
//#import "Employee.h"
NS_ASSUME_NONNULL_BEGIN
@interface Employee (CoreDataProperties)
@property (nullable, nonatomic, retain) NSString *name;
@property (nullable, nonatomic, retain) NSNumber *height;
@property (nullable, nonatomic, retain) NSDate *birthday;
@end
NS_ASSUME_NONNULL_END
  • 在viewController中控制
// 1、初始化数据库操作对象
NSManagedObjectContext *context = [[NSManagedObjectContext alloc] init];
// 2、指定model模型文件
NSManagedObjectModel *model = [NSManagedObjectModel mergedModelFromBundles:nil];
// 3、持久化存储调度器,把数据保存到一个文件,而不是内存
NSPersistentStoreCoordinator *store = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:model];
// 4、告诉Coredata数据库的名字和路径
NSString *doc = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *sqlitePath = [doc stringByAppendingPathComponent:@"company.sqlite"];
[store addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:[NSURL fileURLWithPath:sqlitePath] options:nil error:nil];
context.persistentStoreCoordinator = store;
  • 增删改查
- (IBAction)addEmployee:(id)sender
{
// 创建
// Employee *emp = [[Employee alloc] init];
Employee *emp = [NSEntityDescription insertNewObjectForEntityForName:@"Employee" inManagedObjectContext:_context];
emp.name = @"song";
emp.height = @1.8;
emp.birthday = [NSDate date];

// 存储
NSError *error = nil;
[_context save:&error];
if(error)
{
NSLog(@"%@",error);
}
}

- (IBAction)updataEmployee:(id)sender
{
// FectchRequest 抓取请求对象
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Employee"];
// 设置过滤条件
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name = %@",@"song"];
request.predicate = predicate;
// 执行请求
NSArray *fetch = [_context executeFetchRequest:request error:nil];
for (Employee *emp in fetch) {
emp.height = @1.9;
}
// 存储
NSError *error = nil;
[_context save:&error];
if(error)
{
NSLog(@"%@",error);
}
}

- (IBAction)deleteEmployee:(id)sender
{
// FectchRequest 抓取请求对象
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Employee"];
// 设置过滤条件
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name = %@",@"song"];
request.predicate = predicate;
// 执行请求
NSArray *fetch = [_context executeFetchRequest:request error:nil];
for (Employee *emp in fetch) {
[_context deleteObject:emp];
}
// 存储
NSError *error = nil;
[_context save:&error];
if(error)
{
NSLog(@"%@",error);
}
}

表连接

{
   // 创建两个部门 ios android
    Department *iosDepart = [NSEntityDescription insertNewObjectForEntityForName:@"Department" inManagedObjectContext:_context];
    iosDepart.name = @"ios";
    iosDepart.departNo = @"0001";
    iosDepart.createDate = [NSDate date];

    Department *andrDepart = [NSEntityDescription insertNewObjectForEntityForName:@"Department" inManagedObjectContext:_context];
    andrDepart.name = @"android";
    andrDepart.departNo = @"0002";
    andrDepart.createDate = [NSDate date];

    // 创建两个员工对象 zhangsan属于ios部门 lisi属于android部门
    Employee *zhangsan = [NSEntityDescription insertNewObjectForEntityForName:@"Employee" inManagedObjectContext:_context];
    zhangsan.name = @"zhangsan";
    zhangsan.height = @(1.90);
    zhangsan.birthday = [NSDate date];
    zhangsan.depart = iosDepart;


    Employee *lisi = [NSEntityDescription insertNewObjectForEntityForName:@"Employee" inManagedObjectContext:_context];

    lisi.name = @"lisi";
    lisi.height = @2.0;
    lisi.birthday = [NSDate date];
    lisi.depart = andrDepart;

    // 存储
    NSError *error = nil;
    [_context save:&error];
    if(error)
    {
        NSLog(@"%@",error);
    }
}
- (void)selectEmployee
{
    // 读取ios部门的员工

    // 1.FectchRequest 抓取请求对象
    NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Employee"];

    // 2.设置过滤条件
    NSPredicate *pre = [NSPredicate predicateWithFormat:@"depart.name = %@",@"android"];
    request.predicate = pre;

    // 4.执行请求
    NSError *error = nil;

    NSArray *emps = [_context executeFetchRequest:request error:&error];
    if (error) {
        NSLog(@"error");
    }

    //NSLog(@"%@",emps);
    //遍历员工
    for (Employee *emp in emps) {
        NSLog(@"名字 %@ 部门 %@",emp.name,emp.depart.name);
    }
}
文章目录
  1. 1. CoreData
    1. 1.1. 使用过程
    2. 1.2. 表连接