0

我目前正在尝试调用本地化的 sqlite3 数据库以从多个表中加载特定值。第一个请求是向 fetchAllAmmoForDiamter 发出的,后者又调用 fetchBCForAllAmmoModels。我可以验证我的请求语句是否正确,因为在 SQLite Database Browser 2.0 中将其作为查询语句运行时,它会返回正确的值。但是,在 iOS 应用程序上运行它时,它会为所有内容返回 0。有人知道为什么会这样吗?

 - (NSArray *)fetchAllAmmoForDiameter:(NSNumber *)d
{
    sqlite3 *database;
    NSMutableArray *allAmmoModels = [NSMutableArray new];
    NSString *db = [[NSBundle mainBundle] pathForResource:@"bulletlib" ofType:@"sqlite3"];

    if (sqlite3_open([db UTF8String], &database) == SQLITE_OK) {
        double diameter = [d doubleValue];
        NSString *query = [NSString stringWithFormat:@"SELECT _id, manufacturer_id, weight, length, model, hide_bcs FROM bullets WHERE diameter=%f ORDER BY diameter, manufacturer_id ASC", diameter ];
        sqlite3_stmt *statement;
        if (sqlite3_prepare_v2(database, [query UTF8String], -1, &statement, nil) == SQLITE_OK) {
            while (sqlite3_step(statement) == SQLITE_ROW) {
                int uniqueID = sqlite3_column_int(statement, 0);
                int manufactureID = sqlite3_column_int(statement, 1);
                double weight = sqlite3_column_double(statement, 2);
                double length = sqlite3_column_double(statement, 3);
                char *model = (char *)sqlite3_column_text(statement, 4);

                //            NSLog(@"%d,%d,%f,%f %f, %s", uniqueID, manufactureID, diameter, weight, length, model );

                NSString *modelName = [NSString stringWithUTF8String:model];
                AmmoDatabaseModel *dbModel = [[AmmoDatabaseModel alloc]initWithUniqueId:uniqueID manufactureId:manufactureID diameter:diameter weight:weight length:length andModel:modelName];

                [allAmmoModels addObject:dbModel];
            }

        } else {
            printf( "could not prepare statemnt: %s\n", sqlite3_errmsg(database) );
        }
        sqlite3_finalize(statement);
        sqlite3_close(database);
        [self fetchBCForAllAmmoModels:allAmmoModels];

    }
    return allAmmoModels;

}


-(void)fetchBCForAllAmmoModels:(NSArray *)ammoModels

{
    for (AmmoDatabaseModel *ammoModel in ammoModels ){

        ammoModel.ballisticCoefficients = [self fetchBCForAmmoModel:ammoModel];

    }


}

-(NSArray *)fetchBCForAmmoModel:(AmmoDatabaseModel *)ammoModel
{
    NSMutableArray *bcModels = [NSMutableArray new];
    sqlite3 *database;
    NSString *db = [[NSBundle mainBundle] pathForResource:@"bulletlib" ofType:@"sqlite3"];

    if (sqlite3_open([db UTF8String], &database) == SQLITE_OK) {


        NSString *query =  [NSString stringWithFormat:@"SELECT drag_func, bc, minvel FROM bullet_bcs WHERE bullet_id=%i", [ammoModel.uniqueID intValue]];
        sqlite3_stmt *statement;
        if (sqlite3_prepare_v2(database, [query UTF8String], -1, &statement, nil) == SQLITE_OK) {
            while (sqlite3_step(statement) == SQLITE_ROW) {
                int dragFunction = sqlite3_column_double(statement, 1);
                double bc = sqlite3_column_double(statement, 2);
                int minvel = sqlite3_column_int(statement, 3);
                BallisticCoefficientDatabaseModel *bcModel = [[BallisticCoefficientDatabaseModel alloc] initWithDragFunction:dragFunction coefficient:bc minimumVelocity:minvel];
                [bcModels addObject:bcModel];
            }

        } else {
            printf( "could not prepare statemnt: %s\n", sqlite3_errmsg(database) );
        }
        sqlite3_finalize(statement);
        sqlite3_close(database);

    }
    return bcModels;
}
4

1 回答 1

0

事实证明,我进行了正确的查询并成功获取了我的数据。主要问题是 db 的架构(出于某种疯狂的原因)没有在它应该具有的记录的行和列中提取正确的数据。我在单步使用时打印了我的数据

NSString * str = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 1)];

并将结果与​​它们各自的值对齐。

于 2013-10-28T20:40:12.610 回答