CoreData是蘋果提供的數據持久化的一種,實質仍是經過數據庫存儲數據.可是操做數據的時候再也不須要sql語句,核心文件和類:sql
1.模型文件:也就是.xcdatamodeld,至關於數據庫文件數據庫
2.實體:至關於數據庫的表dom
3.實體類:表存儲的對象的類fetch
4.上下文:關聯模型文件和實際生成的數據庫文件;atom
使用的步驟.net
1.建立一個模型文件:(建立工程use coredata就行)代理
2.建立實體,也就是建立表orm
3.建立實體類對象
4.生成上下文,(操做上下紋也就是操做數據庫)排序
ViewController.m的代碼以下:
#import "ViewController.h"
#import "AppDelegate.h"
#import "Student.h"
@interface ViewController ()
//上下文
@property (nonatomic,strong) NSManagedObjectContext *context;
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//插入數據
// [self insertData];
//數據查詢
//[self selectData];
//刪除數據
//[self deleteData];
//更新數據
[self updataData];
NSLog(@"%@",NSHomeDirectory());
}
#pragma mark - 更新數據
- (void)updataData{
//1.查詢到須要更新的數據
NSFetchRequest *fetchrequest = [[NSFetchRequest alloc]
initWithEntityName:@"Student"];
//建立查詢對象
NSPredicate *pre = [NSPredicate predicateWithFormat:@"score<70"];
//設置查詢條件
fetchrequest.predicate = pre;
//查詢
NSArray *array = [self.context executeFetchRequest:fetchrequest error:nil];
//2.更新屬性
for (Student *stu in array) {
stu.score = @60;
}
//3.保存數據庫
[self.context save:nil];
}
#pragma mark - 刪除數據
- (void)deleteData{
//1.查詢要刪除的數據
NSFetchRequest *fetchrequest = [[NSFetchRequest alloc]
initWithEntityName:@"Student"];
//建立查詢對象
NSPredicate *pre = [NSPredicate predicateWithFormat:@"name = '小明'"];
//設置查詢條件
fetchrequest.predicate = pre;
//查詢
NSArray *array = [self.context executeFetchRequest:fetchrequest error:nil];
//2.刪除數據
for (Student *stu in array) {
[self.context deleteObject:stu];
}
//3.保存數據庫
[self.context save:nil];
}
#pragma mark - 數據查詢
- (void)selectData{
//1.建立一個查詢對象
//參數:實體名:至關於表名
NSFetchRequest *fetchrequest = [NSFetchRequest fetchRequestWithEntityName:@"Student"];
//2.查詢數據
//返回值:查詢結果數據
NSArray *array = [self.context executeFetchRequest:fetchrequest error:nil];
for (Student *stu in array) {
[stu show];
}
//==========按條件查詢==================
//1.建立條件對象
//條件:成績大於80
NSPredicate *pred = [NSPredicate predicateWithFormat:@"score > 80"];
NSPredicate *pred2 = [NSPredicate predicateWithFormat:@"score > %@",@90];
NSPredicate *pred3 = [NSPredicate predicateWithFormat:@"%K > %@",@"age",@15];
//2.設置查詢對象的條件
fetchrequest.predicate = pred3;
//3.查詢數據
NSArray *array2 = [self.context executeFetchRequest:fetchrequest error:nil];
for (Student *stu in array2) {
[stu show];
}
//===============對查詢結果進行排序======================
//1.建立排序對象
//參數1:按哪一個屬性進行排序
//參數2:是否升序
NSSortDescriptor *desc = [NSSortDescriptor sortDescriptorWithKey:@"age" ascending:NO];
NSSortDescriptor *desc1 = [NSSortDescriptor sortDescriptorWithKey:@"score" ascending:NO];
//2.設置查詢的排序
fetchrequest.sortDescriptors = @[desc,desc1];
//3.查詢數據
NSArray *array3 = [self.context executeFetchRequest:fetchrequest error:nil];
for (Student *stu in array3) {
[stu show];
}
}
#pragma mark - 數據的插入
-(void)insertData{
//=============插入單條數據======================
//1.經過實體建立一個學生對象
//參數1:實體名(表名)
//參數2:上下文(數據庫)
Student *student = [NSEntityDescription insertNewObjectForEntityForName:@"Student" inManagedObjectContext:self.context];
//2.設置學生對象的屬性
student.name = @"小明";
student.age = @20;
student.score = @90;
//保存數據庫
[self.context save:nil];
//========插入多條數據===========
for (int i =0 ; i<20; i++) {
//1.建立對象
Student *stu = [NSEntityDescription insertNewObjectForEntityForName:@"Student" inManagedObjectContext:self.context];
//2.設置屬性
stu.name = [NSString stringWithFormat:@"name%d",i];
stu.age = [NSNumber numberWithInt:arc4random() % 10 +10];
stu.score = [NSNumber numberWithFloat:arc4random() % 100];
}
//3.保存數據庫
[self.context save:nil];
}
#pragma mark - 懶加載
- (NSManagedObjectContext *)context{
if (_context == nil) {
//1.拿到當前應用程序的代理
//拿到當前應用程序對象
//生成上下文
AppDelegate *delegate = [UIApplication sharedApplication].delegate;
_context = delegate.managedObjectContext;
}
return _context;
}
@end