SQLite在iOS中混合逻辑运算符OR和AND

时间:2014-04-21 22:45:13

标签: objective-c sqlite operators

我在 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 加载项中正确返回它们。

1 个答案:

答案 0 :(得分:1)

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

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,而是在代码的其他地方。