TheCodingArt TheCodingArt - 5 months ago 8
iOS Question

iOS - requesting data for multiple tables from the same database returning incorrect values

I'm currently attempting to make a call to a localized sqlite3 database to load particular values from multiple tables. The first request is made to fetchAllAmmoForDiamter which in turn calls fetchBCForAllAmmoModels. I can verify that my request statement is correct, as when running it in SQLite Database Browser 2.0 as a query statement, it returns the correct values. When running this on the iOS application though, it returns 0 for everything. Would anyone know why this is occurring?

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

Answer

It turns out I was making the right query and was getting my data successfully. The major issue was the schema for the db (for some crazy(new) reason) wasn't pulling the correct data in the noted rows and columns it should have. I printed out my data while stepping using

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

and aligned the results with their respective values.

Hope this is helpful.

Comments