sqlite3第一次运行INSERT和UPDATE查询,但第二次返回错误

时间:2010-03-07 16:28:25

标签: iphone sqlite

以下代码(来自为每个新视图创建的数据库对象):


-(void)update:(NSString *)queryText{
    NSLog(queryText);
    char *errorMsg;

    if (sqlite3_prepare_v2(database, [queryText UTF8String], -1, &statement, nil) == SQLITE_OK) {
    } else {
        NSLog(@"HMM, COULDNT RUN QUERY: %s\n", sqlite3_errmsg(database));  
    }
    if (sqlite3_step(statement) != SQLITE_DONE){
        NSAssert1(0, @"Error updating table: %s", errorMsg); 
    }
    sqlite3_finalize(statement);
    NSLog(@"Update saved");
}
-(void)insert_answer:(int)obj_id question_type:(NSString *)question_type score:(int)score{
    char *errorMsg;
    char *update = "INSERT INTO Answers (obj_id, question_type, score, date) VALUES (?, ?, ?, DATE())";
    //sqlite3_stmt *stmt;
    if (sqlite3_prepare_v2(database, update, -1, &statement, nil) == SQLITE_OK) { 
        sqlite3_bind_int(statement, 1, obj_id);
        sqlite3_bind_text(statement, 2, [question_type UTF8String], -1, NULL);
        sqlite3_bind_int(statement, 3, score);
    } 
    if (sqlite3_step(statement) != SQLITE_DONE){
        NSAssert1(0, @"Error inserting: %s", errorMsg); 
    }
    sqlite3_finalize(statement);
    NSLog(@"Answer saved");
}
我的应用从此代码中调用


    [db insert_answer:[[dict_question objectForKey:@"JISDec"] intValue] question_type:@"kanji_meaning" score:arc4random() % 100];
    //
    //update EF, Next_question, n here
    //
    [db update:[NSString stringWithFormat:@"UPDATE Kanji SET EF = %d WHERE Kanji = '%@'", [[dict_question objectForKey:@"EF"] intValue] + 2, [dict_question objectForKey:@"question"]]];
    [db update:[NSString stringWithFormat:@"UPDATE Kanji SET Next_Question = %d WHERE Kanji = '%@'", 1, [dict_question objectForKey:@"question"]]];
    [db update:[NSString stringWithFormat:@"UPDATE Kanji SET n = %d WHERE Kanji = '%@'", [[dict_question objectForKey:@"n"] intValue] + 1, [dict_question objectForKey:@"question"]]];

并且在我的应用程序第一次回答问题时运行正常,但下次(加载新视图后)它会返回一个断言错误并崩溃。

有谁知道为什么???我在另一个应用程序中有几乎完全相同的代码,它没有任何问题。

非常感谢。

编辑:添加了堆栈:


2010-03-08 03:01:12.107 Kanji[33864:207] *** Assertion failure in -[databaseConnection update:], /Volumes/Xcode/Kanji/Classes/../databaseConnection.m:58
2010-03-08 03:01:12.108 Kanji[33864:207] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Error updating table: (null)'
2010-03-08 03:01:12.109 Kanji Training with Watanabe Sensei[33864:207] Stack: (
    30135387,
    2561586441,
    30219323,
    814852,
    37730,
    18074,
    2757637,
    3165006,
    3173743,
    3168955,
    2862559,
    2770888,
    2797665,
    37424473,
    29920128,
    29916232,
    37418517,
    37418714,
    2801583
)

1 个答案:

答案 0 :(得分:0)

如果您要显示“断言错误”并且可能是堆栈跟踪以查看实际上失败的呼叫,那将会有所帮助。

另请注意,即使您正在检查错误,也没有正确执行此操作。即使发生错误,您仍继续执行sqlite函数。这是灾难的秘诀。