I’m working with SQlite for the first time….
I look up a discount in the DB, and every things works fine, until i select “nothing”. This happens if the amount (adNumbers) is higher or lower than specified in the DB.
Then when trying to retrieve discount value from the DB column i get an error and application crashes. I have tried to make a NULL check, but that doesn’t seems to help/work.
I have tried to use
while( sqlite3_step(compiledStatement) == SQLITE_ROW){
but the i don’t get anything returned at all…
Please point me in the right direction – how do i check if the DB returns any records (adNumbers are between values en the DB )
This is my method..
-(NSString *) adDisq:(NSString *)strDISQ:(NSInteger)adNumbers{
NSString *strDiscount;
strDiscount = @"00";
if(adSTMT == nil) {
const char *sql = "SELECT ID, DISCOUNT_TYPE, DESCRIPTION, AD_FROM, AD_TO, AD_DISQ FROM DISCOUNT "
"WHERE (((DISCOUNT_TYPE)=?) AND ((?) Between AD_FROM And AD_TO))";
if(sqlite3_prepare_v2(database, sql, -1, &adSTMT, NULL) != SQLITE_OK)
NSAssert1(0, @"Error while creating detail view statement. '%s'", sqlite3_errmsg(database));
}
sqlite3_bind_text(adSTMT, 1, [strDISQ UTF8String], -1, SQLITE_STATIC);
sqlite3_bind_int(adSTMT, 2, adNumbers);
if(SQLITE_DONE != sqlite3_step(adSTMT)) {
NSLog(@"SELECT went OK, getting rows");
const char *columnText = (const char *)sqlite3_column_text(adSTMT, 5);
if(columnText != NULL){
strDiscount = [NSString stringWithUTF8String:(char *)sqlite3_column_text(adSTMT, 5)];
NSLog(@"Discount: %@", strDiscount);
}
else {
strDiscount = @"00";
}
}
else{
NSAssert1(0, @"Error while getting the discount'%s'", sqlite3_errmsg(database));
}
//Reset the detail statement.
sqlite3_reset(adSTMT);
return strDiscount;
}
Problem solved
NSAssert1(0, @”Error while getting the discount’%s'”, sqlite3_errmsg(database));
Was making the crash – as expected – doh !