简体   繁体   中英

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. That is a recipe for disaster.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM