2012-02-22 58 views
10

有一種簡單的方式來獲得一個executeQuery:SELECT * ...的FMDB結果很容易變成一本字典?FMDB結果集到字典

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

我想知道是否有一種方法可以使字典鍵列名和值列值。最好不必在時間內的每一行做一個循環。

回答

27

沒錯:

NSMutableArray *results = [NSMutableArray array]; 

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

-resultDictionaryFMResultSet一個內置的方法,該方法將轉向當前元組到一個NSDictionary,通過柱名作爲關鍵字。

+0

太棒了!你從哪裏得到這個?查看FMDB文檔resultDict不在那裏。 – Bot 2012-02-23 02:18:44

+0

@jostster它在'FMResultSet.h'中。 – 2012-02-23 02:30:03

+0

'-resultDict'已被棄用,請改用'-resultDictionary'。 – lucianf 2012-05-21 09:24:37