views:

80

answers:

2

Hi,

I am new to objective-C and iphone apps.

I am accessing SQLite and have 3 rows in my table "coffee". I used the following way to grab sth out from the table, however, only then 2nd and 3rd rows are being pulled out {the 1st row is always missed}. Is that due to the logic in my while loop by checking while sqlite3_step(selectstmt) returns SQLITE_ROW is wrong? Here is the code:

if (sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK) {

  const char *sql = "select coffeeID, coffeeName from coffee";
  sqlite3_stmt *selectstmt;
  NSLog(@"sqlite_prepare_v2 returns: %i", sqlite3_prepare_v2(database, sql, -1, &selectstmt, NULL));

  if(sqlite3_prepare_v2(database, sql, -1, &selectstmt, NULL) == SQLITE_OK) {

   NSLog(@"sqlite3_step returns: %i", sqlite3_step(selectstmt));
   while(sqlite3_step(selectstmt) == SQLITE_ROW) {

    NSInteger primaryKey = sqlite3_column_int(selectstmt, 0);
    Coffee *coffeeObj = [[Coffee alloc] initWithPrimaryKey:primaryKey];
    coffeeObj.coffeeName = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 1)];
    NSLog(@"this is the coffee name: %@", coffeeObj.coffeeName);
    coffeeObj.isDirty = NO;

    [appDelegate.coffeeArray addObject:coffeeObj];
    [coffeeObj release];
   }
  }
 }

On the other hand, is there any convenient way for me to check the number of rows returen in a query directly from the C interface of SQLite?

Many thanks.

+1  A: 

You could use the query SELECT COUNT(*) FROM coffee to tell you how many rows there are.

And also, save yourself some headaches and use a SQLite wrapper.

Dave DeLong
A: 

Are the 2 sqlite3_step() calls meant to be executed here?

   NSLog(@"sqlite3_step returns: %i", sqlite3_step(selectstmt));
   while(sqlite3_step(selectstmt) == SQLITE_ROW {

BTW: there a parenthesis missing in the while line. Do not rewrite your code for SO. Copy/Paste it to avoid copying errors (pasting errors are much more rare)

pmg