1

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;
}

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

2 Comments

The SQLite C/C++ Introduction may be worth reading.
ty works like a charm, yea that is what happens when you mostly copy paste - you have no clue how SQLite relaly works x_x Sad part is that I have used the step function before too.
0
    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);
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.