tags:

views:

807

answers:

1

I'm trying to get a record count of a table in sqlite3 on the iphone so I can do a little dynamic memory allocation. But, when I run the following code, I get a record count of zero. The DB is open (I have more code immediately following this that retrieves the rows from the table that I'm interested in). I just can't get a record count.

sqlite3_stmt *stmt = nil;

const char *countSql = "select count(*) from users;";
if (sqlite3_prepare(db, countSql, -1, &stmt, NULL) != SQLITE_OK) {
    NSAssert1 (0, @"error preparing count statement", sqlite3_errmsg(db));
} else {
    for (int i = 0; i < 3; i++) {
        usersCnt = sqlite3_column_int(stmt, i);
        NSLog(@"usersCnt %d", usersCnt);
    }
}
sqlite3_finalize(stmt);

The only reasons I'm looping through i (1 - 3) is to check that I'm not getting the count back in a column I don't expect.

The codes prints:

2009-11-15 21:26:17.225 MyDBTest [12759:207] usersCnt 0
2009-11-15 21:26:17.225 MyDBTest [12759:207] usersCnt 0
2009-11-15 21:26:17.225 MyDBTest [12759:207] usersCnt 0
+1  A: 

Your code is missing sqlite3_step()

rep_movsd
it's almost like people wait on SO refreshing the screen ceaselessly, looking for questions they know the answer to :)Thanks a million, it's amazing what myopia does even after a cut and paste.
KevinDTimm
if only I could get the rest of my code reviews done this quickly, life would be GRAND!
KevinDTimm