如何找出SQLITE查询结果是NULL?

时间:2014-07-31 05:49:05

标签: ios objective-c sqlite

我在我的应用中使用了以下方法,

-(int) getMax:(NSString *)subItemName second :(int) tableID
{
    int count1=0;
    sqlite3_stmt *statement;
    NSString *query = [NSString stringWithFormat:@"Select MAX(NoOfItems) from billTable where SubItemName='%@' AND TableID='%d'",subItemName,tableID];
    if (sqlite3_prepare_v2(_database, [query UTF8String], -1, &statement, nil)== SQLITE_OK) {
     while (sqlite3_step(statement) == SQLITE_ROW) {
            count1 = [[NSString stringWithUTF8String:(char *) sqlite3_column_text(statement, 0)]intValue];
        }

        sqlite3_step (statement);
    }
    return count1;
}

正如您所看到的那样,我的查询将返回行数(整数),假设我的查询返回NULL值(如果我的数据库中的where子句条件不存在)返回NULL值。如何检查它在执行查询后返回NULL

1 个答案:

答案 0 :(得分:1)

您可以致电sqlite3_column_type()以了解列值的类型:

if (sqlite3_prepare_v2(_database, [query UTF8String], -1, &statement, nil)== SQLITE_OK) {
    if (sqlite3_step(statement) == SQLITE_ROW) {
        if (sqlite3_column_type(statement, 0) == SQLITE_NULL)
            count1 = -1; // or whatever
        else
            count1 = sqlite3_column_int(statement, 0);
    }
    sqlite3_finalize(statement);
}