CoreData数据库的创建
2015-10-02 12:45 卡农Demo 阅读(178) 评论(0) 收藏 举报CoreData数据库增,删,改,查(在TableView中展示)
一、新建dataModel创建数据库表(导入CoreData类)
再新建NSManagedObject,生成EStudent的类
-
#import <Foundation/Foundation.h> -
#import <CoreData/CoreData.h> -
-
@class NSManagedObject; -
-
@interface EStudent : NSManagedObject -
-
@property (nonatomic, retain) NSNumber * age; -
@property (nonatomic, retain) NSNumber * height; -
@property (nonatomic, retain) NSString * name; -
@property (nonatomic, retain) NSManagedObject *school; -
-
@end -
#import "EStudent.h" -
//#import "NSManagedObject.h" -
#import <CoreData/CoreData.h> -
@implementation EStudent -
-
@dynamic age; -
@dynamic height; -
@dynamic name; -
@dynamic school; -
-
@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];
浙公网安备 33010602011771号