一:先建议两张表
- Person,Score 分别代表,学生表,分数表
- 在 Person的Relationships里面建立关系,指向分数score
二:coreData生成的两个表:
Person
@class Score;@interface Person : NSManagedObject@property (nonatomic, retain) NSString * address;@property (nonatomic, retain) NSNumber * age;@property (nonatomic, retain) NSString * name;@property (nonatomic, retain) Score *score;@end
Score
@interface Score : NSManagedObject@property (nonatomic, retain) NSNumber * dotNet;@property (nonatomic, retain) NSNumber * java;@property (nonatomic, retain) NSNumber * php;@end
三:添加记录查询记录代码
- (void)addTheRowData{ Person *person = [NSEntityDescription insertNewObjectForEntityForName:@"Person" inManagedObjectContext:self.appDelegate.managedObjectContext]; person.name = @"李四"; person.age = [NSNumber numberWithInt:20]; person.address = @"中国济南"; Score *score = [NSEntityDescription insertNewObjectForEntityForName:@"Score" inManagedObjectContext:self.appDelegate.managedObjectContext]; score.dotNet = [NSNumber numberWithFloat:70.77]; score.php = [NSNumber numberWithFloat:80.88]; score.java = [NSNumber numberWithFloat:90.99]; person.score = score; //score.person = person; [self.appDelegate saveContext]; }- (void)showTheTableData{ NSFetchRequest *fetchRequest = [[NSFetchRequest alloc]initWithEntityName:@"Person"]; NSError *error; NSArray *fetchedObjects = [self.appDelegate.managedObjectContext executeFetchRequest:fetchRequest error:&error]; for (Person *info in fetchedObjects) { //Score *score = info.score; NSLog(@"name:%@ age:%@ address:%@ --- dotNet:%@ java:%@ php:%@", info.name,info.age,info.address,info.score.dotNet,info.score.java,info.score.php); } }