FMDB resultset into dictionary
Asked Answered
T

1

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.

Tonita answered 22/2, 2012 at 23:46 Comment(0)
C
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.

Conditioner answered 23/2, 2012 at 2:7 Comment(5)
Awesome! where did you get this? Looking at the FMDB documentation resultDict isn't on there.Tonita
@jostster it's in FMResultSet.h.Conditioner
-resultDict is deprecated, use -resultDictionary instead.Ard
@Ard How will this handle a join query which will contain similar column names from two tables?Nachison
@ShaggyD Check out the code, 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.Ard

© 2022 - 2024 — McMap. All rights reserved.