IOS CoreData 多表查詢demo解析(一)--建表
下面插入測試資料:
Department *dept1 = [NSEntityDescription insertNewObjectForEntityForName:@"Department" inManagedObjectContext:self.managedObjectContext];
dept1.dp_deptname = @"HR";
Department *dept2 = [NSEntityDescription insertNewObjectForEntityForName:@"Department" inManagedObjectContext:self.managedObjectContext];
dept2.dp_deptname = @"DEV";
Department *dept3 = [NSEntityDescription insertNewObjectForEntityForName:@"Department" inManagedObjectContext:self.managedObjectContext];
dept3.dp_deptname = @"POD";
Salary *sy1 =[NSEntityDescription insertNewObjectForEntityForName:@"Salary" inManagedObjectContext:self.managedObjectContext];
sy1.sy_level = @"D";
sy1.sy_scale = 0.1;//[NSNumber numberWithDouble:0.1];
Salary *sy2 =[NSEntityDescription insertNewObjectForEntityForName:@"Salary" inManagedObjectContext:self.managedObjectContext];
sy2.sy_level = @"C";
sy2.sy_scale = 0.15;//[NSNumber numberWithDouble:0.15];
Salary *sy3 =[NSEntityDescription insertNewObjectForEntityForName:@"Salary" inManagedObjectContext:self.managedObjectContext];
sy3.sy_level = @"B";
sy3.sy_scale = 0.4;//[NSNumber numberWithDouble:0.4];
Salary *sy4 =[NSEntityDescription insertNewObjectForEntityForName:@"Salary" inManagedObjectContext:self.managedObjectContext];
sy4.sy_level = @"A";
sy4.sy_scale = 0.8;//[NSNumber numberWithDouble:0.8];
Post *pt1 = [NSEntityDescription insertNewObjectForEntityForName:@"Post" inManagedObjectContext:self.managedObjectContext];
pt1.pt_name = @"行政專員";
pt1.dept = dept1;
pt1.salary = sy2;
Post *pt2 = [NSEntityDescription insertNewObjectForEntityForName:@"Post" inManagedObjectContext:self.managedObjectContext];
pt2.pt_name = @"人事經理";
pt2.dept = dept1;
pt2.salary = sy3;
Post *pt3 = [NSEntityDescription insertNewObjectForEntityForName:@"Post" inManagedObjectContext:self.managedObjectContext];
pt3.pt_name = @"開發工程師";
pt3.dept = dept2;
pt3.salary = sy2;
Post *pt4 = [NSEntityDescription insertNewObjectForEntityForName:@"Post" inManagedObjectContext:self.managedObjectContext];
pt4.pt_name = @"架構師";
pt4.dept = dept2;
pt4.salary = sy3;
Post *pt5 = [NSEntityDescription insertNewObjectForEntityForName:@"Post" inManagedObjectContext:self.managedObjectContext];
pt5.pt_name = @"專案經理";
pt5.dept =dept2;
pt5.salary = sy3;
Post *pt6= [NSEntityDescription insertNewObjectForEntityForName:@"Post" inManagedObjectContext:self.managedObjectContext];
pt6.pt_name = @"測試工程師";
pt6.dept = dept2;
pt6.salary = sy1;
Post *pt7 = [NSEntityDescription insertNewObjectForEntityForName:@"Post" inManagedObjectContext:self.managedObjectContext];
pt7.pt_name = @"銷售代表";
pt7.dept = dept3;
pt7.salary = sy1;
Post *pt8 = [NSEntityDescription insertNewObjectForEntityForName:@"Post" inManagedObjectContext:self.managedObjectContext];
pt8.pt_name = @"銷售經理";
pt8.dept = dept3;
pt8.salary = sy2;
Post *pt9= [NSEntityDescription insertNewObjectForEntityForName:@"Post" inManagedObjectContext:self.managedObjectContext];
pt9.pt_name = @"大客戶經理";
pt9.dept = dept3;
pt9.salary = sy4;
Bank *bk1 = [NSEntityDescription insertNewObjectForEntityForName:@"Bank" inManagedObjectContext:self.managedObjectContext];
bk1.bk_name = @"招行";
bk1.bk_address = @"廣州";
Bank *bk2 = [NSEntityDescription insertNewObjectForEntityForName:@"Bank" inManagedObjectContext:self.managedObjectContext];
bk2.bk_name = @"浦發";
bk2.bk_address = @"上海";
Bank *bk3 = [NSEntityDescription insertNewObjectForEntityForName:@"Bank" inManagedObjectContext:self.managedObjectContext];
bk3.bk_name = @"工行";
bk3.bk_address = @"深圳";
Employee *em1 = [NSEntityDescription insertNewObjectForEntityForName:@"Employee" inManagedObjectContext:self.managedObjectContext];
em1.em_age = 20;//[NSNumber numberWithInt:20];
em1.em_name = @"張三";
em1.em_sex = 1;//[NSNumber numberWithInt:1];
em1.em_bankcardid = @"46326587439043";
em1.dept = dept1;
em1.post = pt1;
em1.bank = bk3;
Employee *em2 = [NSEntityDescription insertNewObjectForEntityForName:@"Employee" inManagedObjectContext:self.managedObjectContext];
em2.em_age = 18;//[NSNumber numberWithInt:18];
em2.em_name = @"李四";
em2.em_sex = 2;//[NSNumber numberWithInt:2];
em2.em_bankcardid = @"32565443246567";
em2.dept = dept1;
em2.post = pt2;
em2.bank = bk3;
Employee *em3 = [NSEntityDescription insertNewObjectForEntityForName:@"Employee" inManagedObjectContext:self.managedObjectContext];
em3.em_age = 26;//[NSNumber numberWithInt:26];
em3.em_name = @"歐文";
em3.em_sex = 2;//[NSNumber numberWithInt:2];
em3.em_bankcardid = @"14354654656767";
em3.dept = dept2;
em3.post = pt3;
em3.bank = bk2;
Employee *em4 = [NSEntityDescription insertNewObjectForEntityForName:@"Employee" inManagedObjectContext:self.managedObjectContext];
em4.em_age = 22;//[NSNumber numberWithInt:22];
em4.em_name = @"張三";
em4.em_sex = 2;//[NSNumber numberWithInt:2];
em4.em_bankcardid = @"9873425837433";
em4.dept = dept2;
em4.post = pt4;
em4.bank = bk2;
Employee *em5 = [NSEntityDescription insertNewObjectForEntityForName:@"Employee" inManagedObjectContext:self.managedObjectContext];
em5.em_age = 28;//[NSNumber numberWithInt:18];
em5.em_name = @"李四";
em5.em_sex = 1;//[NSNumber numberWithInt:2];
em5.em_bankcardid = @"32565743246567";
em5.dept = dept2;
em5.post = pt5;
em5.bank = bk3;
Employee *em6 = [NSEntityDescription insertNewObjectForEntityForName:@"Employee" inManagedObjectContext:self.managedObjectContext];
em6.em_age = 21;//[NSNumber numberWithInt:26];
em6.em_name = @"歐陽";
em6.em_sex = 1;//[NSNumber numberWithInt:2];
em6.em_bankcardid = @"14354614656767";
em6.dept = dept2;
em6.post = pt6;
em6.bank = bk1;
Employee *em7 = [NSEntityDescription insertNewObjectForEntityForName:@"Employee" inManagedObjectContext:self.managedObjectContext];
em7.em_age = 22;//[NSNumber numberWithInt:22];
em7.em_name = @"王飛";
em7.em_sex = 2;//[NSNumber numberWithInt:2];
em7.em_bankcardid = @"9878429837433";
em7.dept = dept3;
em7.post = pt7;
em7.bank = bk1;
Employee *em8 = [NSEntityDescription insertNewObjectForEntityForName:@"Employee" inManagedObjectContext:self.managedObjectContext];
em8.em_age = 26;//[NSNumber numberWithInt:18];
em8.em_name = @"張前";
em8.em_sex = 1;//[NSNumber numberWithInt:2];
em8.em_bankcardid = @"32565443246167";
em8.dept = dept3;
em8.post = pt8;
em8.bank = bk2;
Employee *em9 = [NSEntityDescription insertNewObjectForEntityForName:@"Employee" inManagedObjectContext:self.managedObjectContext];
em9.em_age = 21;//[NSNumber numberWithInt:26];
em9.em_name = @"謝陽";
em9.em_sex = 1;//[NSNumber numberWithInt:2];
em9.em_bankcardid = @"14354694656767";
em9.dept = dept3;
em9.post = pt9;
em9.bank = bk1;
[self saveContext];
資料庫中資料:
IOS CoreData 多表查詢demo解析(三)--查詢