简体   繁体   English

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

[英]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.

相关问题 iOS:选择选项第一次返回空值,而不是第二次返回 - iOS: selecting option returns null first time, not second time 图层动画第一次可以正常运行,但不能在第二次相同的调用上运行 - Layer animation works fine first time, but not on second identical invocation Applepay-canMakePaymentsUsingNetworks首次运行应用程序时返回“否”。 - Applepay - canMakePaymentsUsingNetworks returns NO for the first time app runs. 应用程序运行一次,但第二次崩溃 - Application runs one time but crashes the second time iPhone上的SQLite3 - 插入返回错误代码8尝试编写只读数据库 - SQLite3 on iPhone - insert returns error code 8 attempt to write a readonly database UPDATE不能与FMDB一起运行,可以与sqlite命令行一起正常运行 - UPDATE doesn't run with FMDB, runs fine with sqlite command line 问题-插入文字仅在第一次使用时有效,第二次追加到文字末尾 - Issue - Insert text works only for first time , the second time it appends to the end of the text 如果第一次CoreData为空,则再次执行ExecuteFetchRequest - ExecuteFetchRequest a second time if the first time CoreData was empty SQLite3 更新不起作用 - SQLite3 update not working UITableView(与sqlite3)未更新 - UITableView(with sqlite3) not update
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM