【问题标题】:FMDB resultset into dictionaryFMDB 结果集到字典中
【发布时间】:2012-03-13 09:16:49
【问题描述】:

有没有一种简单的方法可以轻松地将executeQuery:SELECT * ... 的 FMDB 结果放入字典中?

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

我想知道是否有一种方法可以让字典键成为列名,值成为列值。最好不必循环遍历 while 内的每一行。

【问题讨论】:

    标签: objective-c sqlite fmdb


    【解决方案1】:

    是的:

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

    -resultDictionaryFMResultSet 的内置方法,它将当前元组转换为 NSDictionary,以列名为键。

    【讨论】:

    • 太棒了!你从哪里得到这个的?查看 FMDB 文档 resultDict 不在那里。
    • @jostster 它在FMResultSet.h
    • -resultDict 已弃用,请改用-resultDictionary
    • @lucianf 这将如何处理包含来自两个表的相似列名的连接查询?
    • @ShaggyD 查看the code,在这种情况下您将无法使用resultDictionary - 只需遍历列索引,因为您知道按您使用的顺序期望列连接子句。 Further confirmation.
    猜你喜欢
    • 2011-09-20
    • 1970-01-01
    • 1970-01-01
    • 2012-03-13
    • 1970-01-01
    • 1970-01-01
    • 2013-06-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多