[英]sqlite3 runs INSERT and UPDATE queries fine first time, but second time returns error
以下代碼(來自為每個新視圖創建的數據庫對象):
-(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
)
如果您顯示“斷言錯誤”以及可能顯示堆棧跟蹤以查看實際調用失敗的信息,將很有幫助。
另外,請注意,即使您正在檢查錯誤,也不能正確執行。 即使發生錯誤,您也將繼續執行sqlite函數。 那是災難的秘訣。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.