0

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

1
  • I have edited your question, but you might want to use some sort of spell checking within your browser... Commented Oct 17, 2010 at 10:06

2 Answers 2

1

I know this is a very old question but just had this problem of myself and put a very easy method to solve this. Hope this might help other visitors

In Your code

Coffee *coffeeObj = [[Coffee alloc] initWithPrimaryKey:primaryKey];
coffeeObj.aString = [self checkNullForSQLStatmentColoumn:sqlite3_column_text(selectstmt, 0)];



-(NSString *) checkNullForSQLStatmentColoumn : (const unsigned char *) coloumn{
    if(coloumn)
    {
        return [NSString stringWithUTF8String:(const char *)coloumn];
    }
    return @"";
}
Sign up to request clarification or add additional context in comments.

Comments

0

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

2 Comments

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
You should put an if before the while an check if sqlite3_step(selectstmt) is different from NSNull i guess

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.