gpt4 book ai didi

objective-c - 在iOS中使用SQLite混合逻辑运算符OR和AND

转载 作者:行者123 更新时间:2023-12-03 19:44:44 24 4
gpt4 key购买 nike

我在SQLite数据库中有以下数据:

+----+------------+------------+
| id | start_date | end_date |
+----+------------+------------+
| 1 | 2014-04-22 | 2014-04-22 |
| 2 | 2014-04-22 | null |
+----+------------+------------+


我正在尝试像这样的sql中获取两行

NSString *query = @"SELECT * FROM periods WHERE `start_date` <= ? AND (`end_date` >= ? OR `end_date` IS NULL)";
FMResultSet *set = [db executeQuery:query, date, date];


但是它仅返回第一行。

确切的sql会在Firefox的SQLite Manager加载项中正确返回它们两者。

最佳答案

要获取所有行,您只需遍历整个结果集:

NSString    *query = @"SELECT * FROM periods WHERE start_date <= ? AND (end_date >= ? OR end_date IS NULL)";
NSString *date = @"2014-04-22";
FMResultSet *rs = [db executeQuery:query, date, date];

while ([rs next]) {
NSLog(@"%@", [rs resultDictionary]);
}
[rs close];


这对我来说很好。问题不在于您的SQL,而在于代码的其他地方。

关于objective-c - 在iOS中使用SQLite混合逻辑运算符OR和AND,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23207296/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com