TAGS :Viewed: 5 - Published at: a few seconds ago

[ What do I need after an INSERT statement? ]

I have an INSERT statement, followed by a SQLITE3_EXEC, followed by a SELECT statement and another SQLITE3_EXEC. I'm getting an SQLERROR 21 (SQLITE_MISUSE) on the EXEC for the SELECT statement.

Am I missing something between the two EXECs? like a COMMIT, or?

Is it possible that the 21 refers to trying to insert a record that already exists?

    NSString *insertCommand = [NSString stringWithFormat:@"INSERT INTO CardData (CARD_ID, CARD_NAME, CODE_VAL) VALUES ('/%@', '/%@', '/%@')",
                               symbol.data, @"Test Card", symbol.typeName];

    sqlite3_exec(db, [insertCommand UTF8String], NULL, NULL, &errmsg);
    if(errmsg != NULL)
        NSLog(@"insert error: /%@", &errmsg);  //  DEBUGGING ONLY!

    //  now, pull it back out of the d/b and display the data
    const char *sqlStatement = @"select CARD_ID, CARD_NAME, CODE_VAL from CardData";
    sqlite3_stmt *compiledStatement;
    int err = sqlite3_prepare_v2(db, [sqlStatement UTF8String], -1, &compiledStatement, NULL);  // Setup the SQL Statement and compile it for faster access
if(err != SQLITE_OK)
   NSLog(@"prepare error: /%@", err);
    else  { 
        // Loop through the results and add them to the feeds array
        while(sqlite3_step(compiledStatement) == SQLITE_ROW) {

            // Read the data from the result row
        resultText.text = [NSString stringWithFormat:@"\nDatabase: \n%@ \n%@ \n%@", resultText.text, 
            [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 0)],
            [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 1)],
            [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 2)]];
    sqlite3_finalize(compiledStatement);  //  release it...

Answer 1

According to documentation, (1.4 Error Codes->SQLITE_MISUSE) it says that you can get this error if you try to access closed database or calling sqlite_exec with same database pointer from two different threads. Just check out for these 2 possibilities in your case.

Hope it helps.

Answer 2

I have decided to use FMDB... thanks everybody for the suggestions, I appreciate your time.