kgaidis
kgaidis

Reputation: 15579

iOS sqlite will return NULL when using SELECT statement

I'm very confused why the SELECT statement doesn't work correctly. It doesn't give me any errors, just returns null. I know it is writing the string correctly and the right string is there, it's just not reading it correctly. Everything as far as I know is correct because I use the same SQLstmt "method" for many other methods/functions similar to this. This one just doesn't make sense on why it shouldn't work.

- (NSString *)returnNote {
    selStmt=nil;

    NSLog(@"Reading note");

    NSString *SQLstmt = [NSString stringWithFormat:@"SELECT 'Notes' FROM '%@' WHERE Exercises = '%@';", currentRoutine, currentExercise];

    // Build select statements
    const char *sql = [SQLstmt UTF8String];

    if (sqlite3_prepare_v2(database, sql, -1, &selStmt, NULL) != SQLITE_OK) {
        selStmt = nil;
    }

    // Building select statement failed
    if (!selStmt) {
        NSAssert1(0, @"Can't build SQL to read Exercises [%s]", sqlite3_errmsg(database));
    }

    NSString *note = [NSString stringWithFormat:@"%s", sqlite3_column_text(selStmt, 0)];

    sqlite3_reset(selStmt); // reset (unbind) statement

    return note;
}

Upvotes: 1

Views: 2262

Answers (2)

Ravi Kumar Karunanithi
Ravi Kumar Karunanithi

Reputation: 2218

    NSString *querySQLS1 = [NSString stringWithFormat: @"SELECT Notes FROM \"%@\" where Exercises=\"%@\"", currentRoutine, currentExercise];
    sqlite3_stmt *statements;
    const char *query_stmts1 = [querySQLS1 UTF8String];

    if(sqlite3_prepare_v2(UsersDB, query_stmts1, -1, &statement, NULL) == SQLITE_OK)
    {
        NSLog(@"in prepare");
        if (sqlite3_step(statement) == SQLITE_ROW)
        {
            NSLog(@"Query executed");

        } 
        else {
            NSLog(@"in else");
        }

        sqlite3_finalize(statement);
    }

Upvotes: 0

wadesworld
wadesworld

Reputation: 13733

You're not calling sqlite3_step. The statement is never executed.

Upvotes: 1

Related Questions