【问题标题】:How to convert predicates to SQL statement?如何将谓词转换为 SQL 语句?
【发布时间】:2015-08-11 23:22:04
【问题描述】:

从服务器收集数据 3 天后,我的 iOS 应用程序出现问题。我正在尝试查明问题,不知道是数据损坏问题还是代码问题。

我已经将 iOS 数据库下载到我的 Mac 上,并且可以针对它运行 sql 语句。

有没有办法将以下谓词“转换”为 SQL 语句,以便我可以验证数据?

    NSMutableArray *predicates = [[NSMutableArray alloc] init];
    NSDate *twoWeeksago = [[NSCalendar autoupdatingCurrentCalendar] dateByAddingUnit:NSCalendarUnitDay
                                                                           value:-14
                                                                          toDate:[NSDate date]
                                                                         options:0];
    [predicates addObject:[NSPredicate predicateWithFormat:@"isRemoved == 0"]];
    [predicates addObject:[NSPredicate predicateWithFormat:@"(publicationDatetime >= %@) AND (publicationDatetime <= %@)", twoWeeksago, [NSDate date]]];
    [predicates addObject:[NSPredicate predicateWithFormat:@"(source == %@) OR (source == %@)", @"BBC", @"Guardian"]];
    NSPredicate *predicate = [NSCompoundPredicate andPredicateWithSubpredicates:predicates];

【问题讨论】:

  • 您还需要这方面的帮助吗?
  • 当然,知道未来会很好:)
  • 在下面查看我的答案。我真的希望它可以帮助你。

标签: ios sql sqlite


【解决方案1】:

好吧,这里有点摸不着头脑,因为我不知道它对你有什么问题,而且我的 Objective C 和谓词知识是有限的,但我认为你最终得到的是:

where removed = 0
  and (publicationDatetime >= 2weeksAgo) AND (publicationDatetime <= today)
  and (source = 'BBC') OR (source = 'Guardian')

现在我认为它可能会给您带来意想不到的结果的地方是 OR 部分。它的基本意思是,它将带回符合以下条件的记录:

removed = 0
(publicationDatetime >= 2weeksAgo)
(publicationDatetime <= today)
(source = 'BBC')

OR

(source = 'Guardian')

我认为你需要改变的是:

(source = 'BBC') OR (source = 'Guardian')

到这里:

((source = 'BBC') OR (source = 'Guardian'))

将它们放在另一组括号中意味着必须满足所有其他条件,并且还必须满足“BBC”或“Guardian”中的一个。

【讨论】:

  • 谢谢,我暂时接受,直到可以完全测试为止。
猜你喜欢
  • 2017-01-29
  • 2021-10-21
  • 1970-01-01
  • 1970-01-01
  • 2021-12-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多