ios - 如何在 iphone iOS 中使用参数在 SQLITE 中形成 LIKE 语句?

标签 ios iphone sqlite parameters sql-like

例如,我有一个名为 name 的搜索字段。如果用户在搜索字段中键入“John”,我应该检查“Name”字段中的值“John”并选择所有相应的字段值。 'John' 将是从 searchBar.text 中获取的 NSString,它必须作为参数添加到查询中。我构造了一个这样的 sql 语句,但它从来没有用过。

NSString *query = [NSString stringWithFormat:@"select * from Employee where EmployeeName LIKE '%%@%'", searchText.text];
const char *sqlStatement = [query UTF8String];

我也试过

NSString *query = [NSString stringWithFormat:@"select * from Employee where EmployeeName LIKE %'%@'%", searchText.text];
const char *sqlStatement = [query UTF8String];

没有任何效果。

最佳答案

诀窍是在其中获取百分比。这很尴尬,但你可以这样做:

@"select * from Employee where EmployeeName LIKE '%%%@%%'"

%% 是百分号,%@ 是替换字符串。

关于ios - 如何在 iphone iOS 中使用参数在 SQLITE 中形成 LIKE 语句?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10149661/

相关文章:

connection - sqlite 3 可以处理多少个连接?

ios - 如何在 Swift 的 TableView 中进行分页?

ios - 如何使 TableView 在向上滚动时将上面的对象推开?

ios - 扫描二维码后如何打开URL?

iphone - 无法在 UILabel 中访问我的文本属性

ruby-on-rails - rails : three most recent comments with unique users

ios - 在标记 swift 下方添加永久标签

iphone - 如何在不禁用 clipsToBounds 的情况下向 MKMapView 添加阴影

ios - Swift:与相同颜色的 UIView 相比,UINavigationBar 看起来很轻

Android SQLite Journal 行为改变了吗?