getting a record count from a sqlite3 iphone DB
I'm trying to get a reco开发者_StackOverflow社区rd 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
Your code is missing sqlite3_step()
精彩评论