10

Is there an easy way to get the FMDB results of an executeQuery:SELECT * ... easily into a dictionary?

FMResultSet *appointmentResults = [[DataClass getDB] executeQuery:@"SELECT * FROM Appointments WHERE date = ?",currDateString];
while ([appointmentResults next]) {
    //Create dictionary
    //Add dictionary to array for later use
}

I was wondering if there was a way I could make the dictionary keys the column names and the values the column values. Preferably without having to do a loop through every row inside the while.

Bot
  • 11,620
  • 10
  • 68
  • 127

1 Answers1

27

Yep:

NSMutableArray *results = [NSMutableArray array];

FMResultSet *appointmentResults = [[DataClass getDB] executeQuery:@"SELECT * FROM Appointments WHERE date = ?",currDateString];
while ([appointmentResults next]) {
  [results addObject:[appointmentResults resultDictionary]];
}

-resultDictionary is a built-in method on FMResultSet that will turn the current tuple into an NSDictionary, keyed by column name.

Dave DeLong
  • 239,073
  • 58
  • 443
  • 495
  • Awesome! where did you get this? Looking at the FMDB documentation resultDict isn't on there. – Bot Feb 23 '12 at 02:18
  • @jostster it's in `FMResultSet.h`. – Dave DeLong Feb 23 '12 at 02:30
  • `-resultDict` is deprecated, use `-resultDictionary` instead. – lucianf May 21 '12 at 09:24
  • @lucianf How will this handle a join query which will contain similar column names from two tables? – Sagar D Nov 21 '17 at 14:09
  • @ShaggyD Check out [the code](https://github.com/ccgus/fmdb/blob/master/src/fmdb/FMResultSet.m), you won't be able to use `resultDictionary` in this case - just iterate through the column index as you know to expect the columns in the order you used in the join clause. [Further confirmation](https://github.com/ccgus/fmdb/pull/212). – lucianf Nov 21 '17 at 18:50