FMResult 中管理数据库值得学习的代码

来源:互联网 发布:linux查看hba卡信息 编辑:程序博客网 时间:2024/05/20 00:14

FMResult 中管理数据库值得学习的代码

- (NSDictionary *)resultDict {

    

    int num_cols =sqlite3_data_count(statement.statement);

    

    if (num_cols > 0) {

        NSMutableDictionary *dict = [NSMutableDictionarydictionaryWithCapacity:num_cols];

        

        int i;

        for (i = 0; i < num_cols; i++) {

            

            constchar *col_name = sqlite3_column_name(statement.statement, i);

            

            if (col_name) {

                NSString *colName = [NSStringstringWithUTF8String:col_name];

                id value = nil;

                

                // fetch according to type

                switch (sqlite3_column_type(statement.statement, i)) {

                    case SQLITE_INTEGER: {

                        value = [NSNumbernumberWithLongLong:[selflongLongIntForColumnIndex:i]];

                        break;

                    }

                    case SQLITE_FLOAT: {

                        value = [NSNumbernumberWithDouble:[selfdoubleForColumnIndex:i]];

                        break;

                    }

                    case SQLITE_TEXT: {

                        value = [self stringForColumnIndex:i];

                        break;

                    }

                    case SQLITE_BLOB: {

                        value = [self dataForColumnIndex:i];

                        break;

                    }

                }

                

                // save to dict

                if (value) {

                    [dict setObject:value forKey:colName];

                }

            }

        }

        

        return [[dict copy]autorelease];

    }

    else {

        NSLog(@"Warning: There seem to be no columns in this set.");

    }

    

    return nil;

}

原创粉丝点击