tags:

views:

10

answers:

1

Hi everyone,

I guess the best thing to do is to show the code what I'm doing at the result:

This works:

if (sqlite3_column_text(statement, 0)) {
    [marker setObject:[NSNumber numberWithInt: sqlite3_column_int(statement, 0)] forKey:@"id"];
}

This always returns false for the if statement:

if (sqlite3_column_int(statement, 0)) {
    [marker setObject:[NSNumber numberWithInt: sqlite3_column_int(statement, 0)] forKey:@"id"];
}

This returns an incorrect result for the column:

if (sqlite3_column_text(statement, 0)) {
    [marker setObject:[NSNumber numberWithInt: sqlite3_column_text(statement, 0)] forKey:@"id"];
}

Any idea whats going on?

+1  A: 

You should get the number of columns returned before you try to access the value of the column.

int count = sqlite3_column_count (statement);

When you call sqlite3_column_int(), it returns the integer value of the column. What if that value is zero? You can't tell the difference between a valid zero result and a null value for that column. You can test if a column has a null with

sqlite3_column_type () == SQLITE_NULL;

Also, Obj-C does not automatically convert different types, so numberWithInt: sqlite3_column_text will of course give garbage results. What sqlite3_column_text() returns is a pointer to a C string, which is an address in memory where the string resides. I'm guessing you're expecting it to somehow convert the string to an integer? That doesn't happen in C.

lucius