views:

44

answers:

1

Hi all, I am using this in a method:

+ (void) getA:(NSString *)dbPath {

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



        const char *sql = "select a from a_table";
        sqlite3_stmt *selectstmt;
        if(sqlite3_prepare_v2(database, sql, -1, &selectstmt, NULL) == SQLITE_OK) {

            while(sqlite3_step(selectstmt) == SQLITE_ROW) {

                NSInteger primaryKey = sqlite3_column_int(selectstmt, 0);

                Coffee *coffeeObj = [[Coffee alloc] initWithPrimaryKey:primaryKey];
                coffeeObj.aString = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt,0)];
                coffeeObj.isDirty = NO;
                [appDelegate.aArray addObject:coffeeObj.aString];


                [coffeeObj release];
            }
        }
    }
    else
        sqlite3_close(database); 
}

When db has a row, it works fine, but when it has no rows, it just crashes.

What i need to know is, how can I handle my code so that it should work properly when there is no row in DB.
What should I add/modify in my code to behave properly, when there is no rows in the db?

Regards

A: 

Try this: to check if something is not NSNull type if((NSNull *)YourResource != [NSNull null])

JonLOo
thanks JonLOo, but sorry i didnt figured it out, where should i add this? can u point out on my code, to the place where i should i write this?t thanks
shishir.bobby
You should put an if before the while an check if sqlite3_step(selectstmt) is different from NSNull i guess
JonLOo