程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 數據庫知識 >> DB2數據庫 >> DB2教程 >> iosCoreData框架的使用,對上下文數據的增刪改查,表與表之間的關聯,1對多,1對1,謂詞查詢,多表連接

iosCoreData框架的使用,對上下文數據的增刪改查,表與表之間的關聯,1對多,1對1,謂詞查詢,多表連接

編輯:DB2教程

iosCoreData框架的使用,對上下文數據的增刪改查,表與表之間的關聯,1對多,1對1,謂詞查詢,多表連接


 

- (void)viewDidLoad {

[super viewDidLoad];



[self _creatTable];//插入數據



// [self _query];// 查詢數據





// KVC很霸道,即使readonly通過kvc也可賦值,kvo精華

// Book * book = [[Book alloc]init];

//// book.name = @"book1";

// [book setValue:@"book2" forKey:@"name"];

// NSLog(@"%@",book.name);

}



#pragma mark - _query查詢

- (void)_query{

// 初始化模型

NSManagedObjectModel * model = [NSManagedObjectModel mergedModelFromBundles:nil];



// 創建基礎庫協調器

NSPersistentStoreCoordinator * psc = [[NSPersistentStoreCoordinator alloc]initWithManagedObjectModel:model];



// 獲取沙盒路徑

NSString * path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];

path = [path stringByAppendingPathComponent:@"coreData.sqlite"];

NSLog(@"%@",path);

NSURL * url = [[NSURL alloc]initFileURLWithPath:path];



//為協調器加載基礎庫路徑和基礎庫類型

[psc addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:url options:nil error:nil];

// 創建上下文

NSManagedObjectContext * context = [[NSManagedObjectContext alloc]init];

context.persistentStoreCoordinator = psc;





// 初始化查詢請求

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



// 謂詞過濾(查詢條件)

#pragma mark '='

// NSPredicate * predict = [NSPredicate predicateWithFormat:@"name = %@",@"zhangks"];

// NSPredicate * predict = [NSPredicate predicateWithFormat:@"name = 'zhangks'"];

#pragma mark '>'

// NSPredicate * predict = [NSPredicate predicateWithFormat:@"age >10"];

// NSPredicate * predict = [NSPredicate predicateWithFormat:@"name in %@",@[@"zhangsk",@"jack"]];

#pragma mark 'like'模糊查詢

// NSPredicate * predict = [NSPredicate predicateWithFormat:@"name like %@",@"zha*"];

// NSPredicate * predict = [NSPredicate predicateWithFormat:@"name like %@",@"*zha*"];

// NSPredicate * predict = [NSPredicate predicateWithFormat:@"name like %@",@"*zha"];







#pragma mark 'and'和'&&'模糊查詢

// NSPredicate * predict = [NSPredicate predicateWithFormat:@"name = %@ and age = 19",@"zhangks"];

// NSPredicate * predict = [NSPredicate predicateWithFormat:@"name = %@ && age = 19",@"zhangks"];



#pragma mark 'between'模糊查詢

// 方式1

// NSPredicate * predict = [NSPredicate predicateWithFormat:@"age between {20,30}"];



// 方式2

// NSPredicate * predict = [NSPredicate predicateWithFormat:@"age between %@",@[@"10",@"29"]];

// predict = [NSPredicate predicateWithFormat:predict.predicateFormat];



// request.predicate = predict;



// 數據排序 ascending=YES順序,ascending=NO倒序

NSSortDescriptor * sort = [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES];

request.sortDescriptors = @[sort];



NSError * error ;

// 執行查詢語句

NSArray * array = [context executeFetchRequest:request error:&error];// 查詢結果返回的是數組

// NSInteger count = [context countForFetchRequest:request error:&error];

// 查詢結果的個數,返回nsinteger





// NSLog(@"%li",count);



/*

// // KVC方式獲取

// for (NSManagedObject * student in array) {

// NSLog(@"————————————————————%@,%li",[student valueForKey:@"name"],[[student/Users/zhangxin/Desktop/OC/UI/5.19/5.21coreData/testtestcoredata/testtestcoredata/ViewController.m valueForKey:@"age"] integerValue]);

// }



// // 子類KVC方式獲取

// for (Student * student in array) {

// NSLog(@"%li,%@",[student.age integerValue],[student valueForKey:@"name"]);

// }

*///KVC



// 通過映射對象獲取

for (Teacher * teacher in array) {

NSLog(@"%@",teacher.name);

}





}



#pragma mark _creatTable

- (void)_creatTable{

// 初始化模型

NSManagedObjectModel * model = [NSManagedObjectModel mergedModelFromBundles:nil];



// 創建基礎庫協調器

NSPersistentStoreCoordinator * psc = [[NSPersistentStoreCoordinator alloc]initWithManagedObjectModel:model];



// 獲取沙盒路徑

NSString * path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];

path = [path stringByAppendingPathComponent:@"coreData.sqlite"];

NSLog(@"%@",path);

NSURL * url = [[NSURL alloc]initFileURLWithPath:path];



//為協調器加載基礎庫路徑和基礎庫類型

[psc addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:url options:nil error:nil];

// 創建上下文

NSManagedObjectContext * context = [[NSManagedObjectContext alloc]init];

context.persistentStoreCoordinator = psc;





// // 插入數據

// NSManagedObject * student = [NSEntityDescription insertNewObjectForEntityForName:@"Student" inManagedObjectContext:context];

// [student setValue:@"zhangks" forKey:@"name"];

// [student setValue:@(19) forKey:@"age"];

//

// NSManagedObject * teacher = [NSEntityDescription insertNewObjectForEntityForName:@"Teacher" inManagedObjectContext:context];

// [teacher setValue:@"zhqo" forKey:@"name"];



// 插入數據 新建 NSManagedObject subclass類 ,自動根據model中entity(實體)的表創建出類(繼承自NSManagedObject,擁有NSManagedObject的屬性和方法)所以直接用類名創建即可

// Student * student = [NSEntityDescription insertNewObjectForEntityForName:@"Student" inManagedObjectContext:context];

// student.name = @"jack";

// student.age = @(29);

//

//

//

// Teacher * teacher = [NSEntityDescription insertNewObjectForEntityForName:@"Teacher" inManagedObjectContext:context];

// teacher.name = @"limei";



// 初始化查詢請求

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



// 謂詞過濾(查詢條件)

// NSPredicate * predict = [NSPredicate predicateWithFormat:@"name = 'limei'"];



// request.predicate = predict;



NSArray * array = [context executeFetchRequest:request error:nil];

for (Teacher * teacher in array) {

Student * student1 = [NSEntityDescription insertNewObjectForEntityForName:@"Student" inManagedObjectContext:context];

student1.name = @"jim";

student1.age = @(12);

student1.relationship = teacher;













NSLog(@"%@,%@",teacher.name,student1.relationship.name);

//// teacher.name = @"liuwu";

//// [context deleteObject:teacher];

//

}



// NSSet * deleteSet = [context deletedObjects];// 刪除(未save),執行刪除語句後未保存前,緩存中(庫表未修改)存儲的刪除數據

// NSSet * insertSet = [context insertedObjects];//同上



// 保存

NSError * error;

[context save:&error];

}

ios CoreData框架的使用,對上下文數據的增刪改查,表與表之間的關聯,1對多,1對1,謂詞查詢,多表連接ios CoreData框架的使用,對上下文數據的增刪改查,表與表之間的關聯,1對多,1對1,謂詞查詢,多表連接ios CoreData框架的使用,對上下文數據的增刪改查,表與表之間的關聯,1對多,1對1,謂詞查詢,多表連接ios CoreData框架的使用,對上下文數據的增刪改查,表與表之間的關聯,1對多,1對1,謂詞查詢,多表連接ios CoreData框架的使用,對上下文數據的增刪改查,表與表之間的關聯,1對多,1對1,謂詞查詢,多表連接ios CoreData框架的使用,對上下文數據的增刪改查,表與表之間的關聯,1對多,1對1,謂詞查詢,多表連接ios CoreData框架的使用,對上下文數據的增刪改查,表與表之間的關聯,1對多,1對1,謂詞查詢,多表連接

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved