iphone:创建一个查询

时间:2010-01-27 10:26:44

标签: iphone

在我的sqlite数据库表中,有名为id,firstName,middle,lastName

的字段

现在我想按以下方式运行查询

MyQuery = [NSString stringWithFormat:@"select id,(firstName || middlename || lastname) as Name  from Students where firstname like '%@' or middlename like '%@' or lastname like '%@'",searchBarString,searchBarString,searchBarString];

以上查询工作得很好。但我想在第一个和中间名之间添加一个空格(也是中间名和姓)。那我怎么能完成这个任务呢?请建议

2 个答案:

答案 0 :(得分:1)

MyQuery = [NSString stringWithFormat:@"select id,(firstName || ' ' || middlename || ' ' || lastname) as Name  from Students where firstname like '%@' or middlename like '%@' or lastname like '%@'",searchBarString,searchBarString,searchBarString];

试试这个!

答案 1 :(得分:0)

您可以简单地在单独的变量中返回三列,然后在显示它们时将它们连接起来:

NSString *lastName = ...;
NSString *middleName = ...;
NSString *firstName = ...;

NSString *joined = [NSString stringWithFormat:@"%@ %@ %@",lastName,middleName,firstName];

这样做的好处是,您可以按UI中的任意三列进行排序,而无需进行新的sqlite查询,并允许用户编辑这些列。