doxsi
doxsi

Reputation: 1014

Xcode - sql Select a single value does not work

In my app, I trying to get the single id from data base using the query:

SELECT _id FROM rules  where codigo_rest = 2345

I am passing that query to the following function:

-(NSString *)selectIDrest:(NSString *)query{
    NSString * retval;

    sqlite3_stmt *statement;
    if (sqlite3_prepare_v2(_database, [query UTF8String], -1, &statement, nil)
        == SQLITE_OK) {
        while (sqlite3_step(statement) == SQLITE_ROW) {
            int uniqueId = sqlite3_column_int(statement, 0);
            NSLog(@"int %i",uniqueId);

            retval = [NSString stringWithFormat:@"%d",uniqueId];
        }
        sqlite3_finalize(statement);
    }
    NSLog(@"%@",retval);

    return retval;
}

but the retval is alway 0., while table content id is different (1,2,3,4 ...etc). By NSlogs, I realize that while (sqlite3_step(statement) == SQLITE_ROW) is never executed. What is my fault?

Upvotes: 0

Views: 65

Answers (1)

shf301
shf301

Reputation: 31394

You are never executing the query, you only prepared it to be executed. You need to call sqllite3_step to read the first row. The you can call sqllit3_column_int.

You can see how to call it in this tutorial: http://www.raywenderlich.com/913/sqlite-tutorial-for-ios-making-our-app

Upvotes: 2

Related Questions