#import "ViewController.h"
#import "Student.h"
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
{
NSArray *_dataArray;
//负责应用与数据库交互
NSManagedObjectContext *_context;
NSIndexPath *_indexPath;
}
@property (weak, nonatomic) IBOutlet UITextField *nameTextField;
@property (weak, nonatomic) IBOutlet UITextField *ageTextField;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self initManagerObjectContext];
}
-(void)initManagerObjectContext
{
//1.conredata文件的路径
NSString *path = [[NSBundle mainBundle] pathForResource:@"CoreData.momd" ofType:nil];
//文件路径
NSURL *fileUrl = [NSURL fileURLWithPath:path];
//2.加载本地所有模型
NSManagedObjectModel *model = [[NSManagedObjectModel alloc] initWithContentsOfURL:fileUrl];
//3.创建数据库存储协议器(sqlit数据存储方式)
NSPersistentStoreCoordinator *psc = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:model];
NSString *storePath = [NSHomeDirectory() stringByAppendingString:@"/Documents/student.sqlite"];
NSLog(@"%@",NSHomeDirectory());
NSURL *storeUrl = [NSURL fileURLWithPath:storePath];
NSError *error;
NSPersistentStore *store = [psc addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:nil error:&error];
if (!store) {
NSLog(@"%@",error.localizedDescription);
}
//创建数据库管理对象
_context = [[NSManagedObjectContext alloc] init];
_context.persistentStoreCoordinator = psc;
}
- (IBAction)addToSqlite:(id)sender {
Student *model = [NSEntityDescription insertNewObjectForEntityForName:@"Student" inManagedObjectContext:_context];
model.name = _nameTextField.text;
model.age = @(_ageTextField.text.integerValue);
NSError *error;
if (![_context save:&error]) {
NSLog(@"%@",error.debugDescription);
}
}
- (IBAction)selectFromSqlite:(id)sender {
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Student"];
//request.predicate = [NSPredicate predicateWithFormat:@""];
_dataArray = [_context executeFetchRequest:request error:nil];
[_tableView reloadData];
//满足条件的个数
[_context countForFetchRequest:request error:nil];
}
- (IBAction)update:(id)sender {
Student *model = _dataArray[_indexPath.row];
model.name = _nameTextField.text;
model.age = @(_ageTextField.text.integerValue);
[_context save:nil];
}
- (IBAction)delete:(id)sender {
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Student"];
NSArray *results = [_context executeFetchRequest:request error:nil];
for (Student *model in results) {
[_context deleteObject:model];
}
//删除所有数据,可以直接删除数据库文件
}
#pragma mark --UITabelViewDelegate
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return _dataArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIde = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIde];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIde];
}
Student *student = _dataArray[indexPath.row];
cell.textLabel.text = [NSString stringWithFormat:@"姓名:%@",student.name];
cell.detailTextLabel.text = [NSString stringWithFormat:@"年龄:%@",student.age];
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
Student *model = _dataArray[indexPath.row];
_nameTextField.text = model.name;
_ageTextField.text = model.age.stringValue;
_indexPath = indexPath;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end