In my app I'm using FMDB
to query my sqlite
database. I'm passing variables from one view to another and at the end all the selections the variables are filled with the values selected. On the results page I can show these values in label's fine. But the moment I pass them to FMDB
to query the database I don't get any values returned. It crashes and says that the array is 0 which I know it's not.
Code sample below.
- (NSMutableArray *) getChoices
{
NSMutableArray *choices = [[NSMutableArray alloc] init];
FMDatabase *db = [FMDatabase databaseWithPath:[Utility getDatabasePath]];
[db open];
NSString *capsChoiceOne = @"CHOICE 1";
NSString *capsChoiceTwo = @"CHOICE 2";
NSString *capsChoiceThree = @"CHOICE 3";
NSString *capsChoiceFour = @"CHOICE 4";
FMResultSet *results = [db executeQueryWithFormat:@"SELECT * FROM allitems WHERE choice1=%@ AND choice2=%@ AND choice3=%@ AND choice4=%@"
,capsChoiceOne,capsChoiceTwo,capsChoiceThree,capsChoiceFour];
while([results next])
{
Choices *choice = [[Choices alloc] init];
choice.result = [results stringForColumn:@"result"];
[choices addObject:choice];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Your Result"
message:choice.result
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
}
[db close];
return choices;
Now the above code will bring back the result in the Alert View. But the moment I change one of the values to a variable it crashes and say's there was 0 results in my array. I've put the code below for how I'm inserting the variable.
NSString *capsChoiceOne = self.choiceOneSelected.uppercaseString;
the self.choiceOneSelected.uppercaseString
contains the same as the hard coded version but doesn't work.
Any help would be grateful.
Thank you
Check whether self.choiceOneSelected.uppercaseString;
is not nil
.
Also your query have some issues, you need to wrap string
values inside '
So use:
FMResultSet *results = [db executeQueryWithFormat:@"SELECT * FROM allitems WHERE choice1='%@' AND choice2='%@' AND choice3='%@' AND choice4='%@'"
,capsChoiceOne,capsChoiceTwo,capsChoiceThree,capsChoiceFour];