码迷,mamicode.com
首页 > 数据库 > 详细

CoreData数据库的创建

时间:2015-10-02 13:40:28      阅读:278      评论:0      收藏:0      [点我收藏+]

标签:

CoreData数据库增,删,改,查(在TableView中展示)

 

一、新建dataModel创建数据库表(导入CoreData类)

再新建NSManagedObject,生成EStudent的类

 

  1. #import <Foundation/Foundation.h>

  2. #import <CoreData/CoreData.h>

  3.  

  4. @class NSManagedObject;

  5.  

  6. @interface EStudent : NSManagedObject

  7.  

  8. @property (nonatomic, retain) NSNumber * age;

  9. @property (nonatomic, retain) NSNumber * height;

  10. @property (nonatomic, retain) NSString * name;

  11. @property (nonatomic, retain) NSManagedObject *school;

  12.  

  13. @end

  14. #import "EStudent.h"

  15. //#import "NSManagedObject.h"

  16. #import <CoreData/CoreData.h>

  17. @implementation EStudent

  18.  

  19. @dynamic age;

  20. @dynamic height;

  21. @dynamic name;

  22. @dynamic school;

  23.  

  24. @end

 

二、自定义Xib,建立增删该查视图

 

技术分享

 

注意tableView的delegate设置

 

技术分享

 

 

三、创建数据库

 

- (void)prepareCoreData

{

    //a.创建数据库 - 创建一个模型文件

    //b.创建表    - 创建Entity

    //c.构建实体类 Model

    //d.编写代码初始化框架

    

    

    //1.根据模型文件,创建NSManagedObjectModel模型类

    NSURL * fileURL = [[NSBundle mainBundle] URLForResource:@"Main"withExtension:@"momd"];

    

    NSManagedObjectModel * managedObjectModel = [[NSManagedObjectModelalloc] initWithContentsOfURL:fileURL];

    

    //2.关联模型类和数据库文件

    //2.1 创建持久化协调器

    NSPersistentStoreCoordinator * coordinator = [[NSPersistentStoreCoordinator alloc]initWithManagedObjectModel:managedObjectModel];

    

    //2.2 给协调器指定持久化类

    NSString * dbPath = [NSHomeDirectory()stringByAppendingPathComponent:@"Documents/main.sqlite"];

   NSLog(@"Path : %@",dbPath);

    

    NSError * error = nil;

    [coordinator addPersistentStoreWithType:NSSQLiteStoreTypeconfiguration:nil URL:[NSURL fileURLWithPath:dbPath] options:nilerror:&error];

    if (error) {

        NSLog(@"%@",error);

    }

    

    //3. 创建NSManagedObjectContext上下文

    self.context = [[NSManagedObjectContext alloc] init];

    self.context.persistentStoreCoordinator = coordinator;

}

 

- (void)initialDataSource

{

    self.dataSource = [NSMutableArray new];

    

    //读取数据

    //1.创建NSFetchRequest,抓取数据请求类

    NSFetchRequest * request = [[NSFetchRequest alloc]initWithEntityName:@"EStudent"];

    

    

    NSError * error = nil;

    //2.从当前上下文中抓取数据

    NSArray * array = [self.context executeFetchRequest:requesterror:&error];

    if (error) {

        NSLog(@"%@",error);

        return;

    }

    

    [self.dataSource addObjectsFromArray:array];

}


四、自定义tableViewCell的内容

技术分享

 

#pragma mark - tableView Delegate

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

   

   return self.dataSource.count;

}

 

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    

    MyTableViewCell * cell = [tableViewdequeueReusableCellWithIdentifier:@"Cell"];

    if (!cell) {

//        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"Cell"];

        cell = [[[NSBundle mainBundle] loadNibNamed:@"MyTableViewCell"owner:self options:nil]firstObject];

        

    }

    

    EStudent * s = [self.dataSource objectAtIndex:indexPath.row];

//    cell.textLabel.text = s.name;

//   cell.detailTextLabel.text = [NSString stringWithFormat:@"%@",s.age];

    cell.nameTF.text = s.name;

    cell.heightTF.text = [NSString stringWithFormat:@"%@",s.height];

    cell.ageTF.text = [NSString stringWithFormat:@"%@",s.age];

    

    

    return cell;

 

}

 

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

{

    return 90;

}

 

五、增删改查操作

???增

 

//插入操作

    //1.由NSEntityDescription类创建出实体类

    //arg1.实体名(表名,实体类的名字) ;arg2. 上下文

    EStudent * s = [NSEntityDescriptioninsertNewObjectForEntityForName:@"EStudent"inManagedObjectContext:self.context];

    

    s.name = self.nameTF.text;

    s.age = [NSNumber numberWithInt:self.ageTF.text.intValue];

    s.height = [NSNumbernumberWithFloat:self.heightTF.text.floatValue];

    

    //2.存入数据库

    NSError * error = nil;

    [self.context save:&error];

    

    [self.dataSource addObject:s];

    [self.tableView reloadData];

 

???删

 

NSIndexPath * path = [self.tableView indexPathForSelectedRow];

    //删除

    //1.找到需要删除的对象

    EStudent * student = [self.dataSource objectAtIndex:path.row];

    

    //2.从上下文中删除对象

    [self.context deleteObject:student];

    

    //3.保存上下文到文件

    [self.context save:nil];

    

    [self.dataSource removeObject:student];

    [self.tableView reloadData];



???改

 

 //改数据

    //1.找到需要修改的对象

    NSIndexPath * path = [self.tableView indexPathForSelectedRow];

    EStudent * student = [self.dataSource objectAtIndex:path.row];

 

    //2.修改

    student.name = self.nameTF.text;

    

    //3.保存

    [self.context save:nil];

    

    [self.tableView reloadData];



???查

 

//查询

   //1.创建查询请求类

NSFetchRequest * request = [[NSFetchRequest alloc]initWithEntityName:@"EStudent"];

    

    //2.设置筛选条件

    // =

    // CONTAINS

    // BEGINSWITH

    // ENDSWITHD

    NSPredicate * predicate = [NSPredicate predicateWithFormat:@"name CONTAINS %@",self.nameTF.text];

    request.predicate = predicate;

    

    //n.

    NSError * error = nil;

    NSArray * array = [self.context executeFetchRequest:requesterror:&error];

    if (error) {

        NSLog(@"%@",error);

    }

    

    [self.dataSource removeAllObjects];

    [self.dataSource addObjectsFromArray:array];

    [self.tableView reloadData];

 

CoreData数据库的创建

标签:

原文地址:http://www.cnblogs.com/zhuoqi/p/4852120.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!