userMod2
userMod2

Reputation: 8960

SQLite Go through each column in Row

I have a sqlite statement that provides me with a selected single row and 20 columns.

Up to now I've been using this while loop:

while(sqlite3_step(statement) == SQLITE_ROW) {
    NSString *name = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, an_incrementing_int)];
    ...
    }

However the problem with this is as there is only one row it will naturally only return the 1st column.

So is there something like while.. == SQLITE_COLUMN ?

Thanks

Upvotes: 0

Views: 290

Answers (1)

LS_ᴅᴇᴠ
LS_ᴅᴇᴠ

Reputation: 11161

To get number of column a query returns, use sqlite3_column_count.

Function to return column data, sqlite3_column_... all accept an 2nd argument which is int column index.

NSString coldata;
int i;
while(sqlite3_step(statement) == SQLITE_ROW) {
    for (i=0; i<sqlite3_column_count(statement); ++i) {
        coldata= [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, i)];
    }
}

Note also: take care using data pointer to column values!

The pointers returned are valid until a type conversion occurs as described above, or until sqlite3_step() or sqlite3_reset() or sqlite3_finalize() is called. The memory space used to hold strings and BLOBs is freed automatically. Do not pass the pointers returned from sqlite3_column_blob(), sqlite3_column_text(), etc. into sqlite3_free().

Upvotes: 1

Related Questions