2 nicholastas Nicholastas 于 2014.08.08 10:21 提问

iOS 数据库第二。三张表不能插入数据,第一张可以,为什么
  • (id)init
    {
    self = [super init];
    if (self) {

    NSArray * arr = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    _filePath = [arr objectAtIndex:0];
    
    _filePath = [_filePath stringByAppendingPathComponent:@"Test.db"];
    
    NSLog(@"filePath is %@",_filePath);
    _dataBase = [FMDatabase databaseWithPath:_filePath];
    
    if (![_dataBase open]) {
        NSLog(@"can not open dataBase!");
        return nil;
    }
    NSArray *array=[NSArray arrayWithObjects:
                    @"CREATE TABLE if not exists student (Num integer PRIMARY KEY AUTOINCREMENT,studentNumber text,studentName text,studentAddress text)",
                    @"CREATE TABLE if not exists course (b integer PRIMARY KEY AUTOINCREMENT,courseNumber text,courseName text,courseTeacher text)",
                    @"CREATE TABLE if not exists score (a integer PRIMARY KEY AUTOINCREMENT,studentNumber text,courseName text,score text)", nil];
    
    for (NSString * sql in array) {
        [_dataBase executeUpdate:sql];
    }
    

    }
    return self;
    }

  • (BOOL)insertDataToDataBase:(id)model andKey:(NSInteger)key;
    {

    if ([model isKindOfClass:[StedentModel class]]) {

    StedentModel *sModel = (StedentModel *)model;
    
    return [_dataBase executeUpdate:@"INSERT INTO student (Num,studentNumber,studentName,studentAddress) VALUES (?,?,?,?)",[NSNumber numberWithInt:key],sModel.studentNumber,sModel.studentName,sModel.studentAddress];
    

    }
    if ([model isKindOfClass:[CourseModel class]]){

    CourseModel *cModel = (CourseModel *)model;
    return [_dataBase executeUpdate:@"INSERT INTO course (b,courseNumber,courseName,courseTeacher,) VALUES (?,?,?,?)",[NSNumber numberWithInt:key],cModel.courseNumber,cModel.courseName,cModel.courseTeacher];
    

    }
    if ([model isKindOfClass:[ScoreModel class]]){

    ScoreModel *sModel = (ScoreModel *)model;
    return [_dataBase executeUpdate:@"INSERT INTO score (a,studentNumber,courseName,score,) VALUES (?,?,?,?)",[NSNumber numberWithInt:key],sModel.studentNumber,sModel.courseName,sModel.score];
    

    }else{
    return YES;
    }

}
Nicholas 09:59:47

  • (IBAction)saveButtonPressed:(UIButton *)sender {

    StedentModel *stuModel = [[StedentModel alloc]init];
    CourseModel *couModel = [[CourseModel alloc]init];
    ScoreModel *scoModel = [[ScoreModel alloc]init];

    stuModel.studentNumber = _sNumberTextField.text;
    stuModel.studentName = _sNameTextField.text;
    stuModel.studentAddress = _sAddressTextField.text;

    couModel.courseNumber = _cNumberTextField.text;
    couModel.courseName = _cName.text;
    couModel.courseTeacher = _cTeacherTextField.text;

    scoModel.studentNumber = _scoreNumberTextField.text;
    scoModel.courseName = _scoreNameTextField.text;
    scoModel.score = _scoreTextField.text;

    DateBaseSimple *dateBase = [DateBaseSimple sharedDataBase];
    [dateBase insertDataToDataBase:couModel andKey:4];
    [dateBase insertDataToDataBase:scoModel andKey:4];
    [dateBase insertDataToDataBase:stuModel andKey:4];

}

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!