如果我没有从SQLite获取记录,我的应用程序崩溃了

时间:2012-06-05 11:33:18

标签: iphone sqlite select

我第一次与SQlite合作.... 我在数据库中查找折扣,一切正常,直到我选择“无”。如果金额(adNumbers)高于或低于DB中指定的值,则会发生这种情况。

然后,当尝试从DB列检索折扣值时,我收到错误并且应用程序崩溃。我试图进行NULL检查,但这似乎没有帮助/工作。

我试过用 while( sqlite3_step(compiledStatement) == SQLITE_ROW){

但我根本没有得到任何回报......

请指出正确的方向 - 如何检查数据库是否返回任何记录(adNumbers介于数据库的值之间)

这是我的方法..

-(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;

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;

1 个答案:

答案 0 :(得分:0)

问题解决了

NSAssert1(0,@“获取折扣'%s'时出错”,sqlite3_errmsg(数据库));

正在制造崩溃 - 正如所料 - doh!