[英]sqlite3 runs INSERT and UPDATE queries fine first time, but second time returns error
the following code (from a database object created for each new view): 以下代码(来自为每个新视图创建的数据库对象):
-(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");
}
is called by my app from this code: 由我的应用从以下代码调用:
[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"]]];
and runs fine the first time a question is answered in my app, but the next time (after a new view is loaded) it returns an assert error and crashes. 并在我的应用程序中第一次回答问题时运行良好,但是下次(加载新视图后)会返回断言错误并崩溃。
Does anyone know why??? 有谁知道为什么??? I have almost exactly the same code in another app and it has no problems.
我在另一个应用程序中具有几乎完全相同的代码,并且没有问题。
Many thanks. 非常感谢。
EDIT: Added stack: 编辑:添加堆栈:
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
)
It would help if you would show the 'assert error' and maybe a stacktrace to see what call actually fails. 如果您显示“断言错误”以及可能显示堆栈跟踪以查看实际调用失败的信息,将很有帮助。
Also, note that even though you are checking for errors, you are not doing it correctly. 另外,请注意,即使您正在检查错误,也不能正确执行。 Even if an error happens, you continue to exeute sqlite functions.
即使发生错误,您也将继续执行sqlite函数。 That is a recipe for disaster.
那是灾难的秘诀。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.