我有一个类似的枚举:
typedef NS_ENUM(int, name) {
kAll,
kFirstType,
kSecondType
};
然后我做了:
if (type == kAll) {
query = [NSString stringWithFormat:@"SELECT * FROM TABLE WHERE ID = %ld", itemID];
}
else {
query = [NSString stringWithFormat:@"SELECT * FROM TABLE WHERE ID = %ld AND type = %ld", itemID, type];
}
如何将其组合成一个查询,如:
"SELECT CASE WHEN type = 0 THEN (SELECT ALL ITEMS) ELSE '* WHERE ID = blah AND type = meh' END;
如何使用查询执行此操作?我不想要if (blah) then query = meh.. else query = other..
答案 0 :(得分:2)
你可以这样做:
select * from myTable where id = :id and (:type = 0 or :type = type)
或者这个:
select * from myTable where id = ? and (? in (0, type))
但我不希望第二个特别表现良好。我没有测试过任何一个版本。
无法从语句中更改WHERE
子句的结构。你需要某种eval
函数,它不是SQLite内置的。如果需要,您可以找到可加载的eval
扩展程序in the SQLite source tree的源代码,但这看起来有点矫枉过正。