core data 查询条件 NSPredicate使用
2014-01-08 16:08
369 查看
NSPredicate用于查询和过滤
在SQL中作为查询条件通常用WHERE,但在COREDATA中作为查询条件就可以用到NSPredicate.
NSPredicate 不单可以和COREDATA中的FetchRequest 配合使用。也可以与NSArray配合使用。
NSPredicate 中支持的关键词和条件符:
1、>,<,>=,<=,= 比较运算符。
如:
NSPredicate * qcondition= [NSPredicate predicateWithFormat:@"salary >= 10000"];
2、字符串操作(包含):BEGINSWITH、ENDSWITH、CONTAINS
如:
@"employee.name BEGINSWITH[cd] '李'" //姓李的员工
@"employee.name ENDSWITH[c] '梦'" //以梦结束的员工
@"employee.name CONTAINS[d] '宗'" //包含有"宗"字的员工
注:[c]不区分大小写[d]不区分发音符号即没有重音符号[cd]既不区分大小写,也不区分发音符号。
3、范围:IN ,BWTEEN
如:
@"salary BWTEEN {5000,10000}"
@"em_dept IN '开发'"
4、自身:SELF,这个只针对字符数组起作用。
如:
NSArray * test = =[NSArray arrayWithObjects: @"guangzhou", @"beijing", @"shanghai", nil];
@"SELF='beijing'"
5、通配符:LIKE
LIKE 使用?表示一个字符,*表示多个字符,也可以与c、d 连用。
如:
@"car.name LIKE '?he?'" //四个字符中,中间为he
@"car.name LIKE '*jp'" //以jp结束
6、正则表达式:MATCHES
如:
NSString *regex = @"^E.+e$";//以E 开头,以e 结尾的字符。
NSPredicate *pre= [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
if([pre evaluateWithObject: @"Employee"]){
NSLog(@"matches YES");
}else{
NSLog(@"matches NO");
}
7、逻辑运算符:AND、OR、NOT
如:
@"employee.name = 'john' AND employee.age = 28"
8、占位符:
NSPredicate *preTemplate = [NSPredicate predicateWithFormat:@"name==$NAME"];
NSDictionary *dic=[NSDictionary dictionaryWithObjectsAndKeys:
@"Name1", @"NAME",nil];
NSPredicate *pre=[preTemplate predicateWithSubstitutionVariables: dic];
占位符就是字典对象里的key,因此你可以有多个占位符,只要key 不一样就可以了。
query by page
[fetchRequest setFetchLimit:20];
[fetchRequest setFetchOffset: page * 20];
query by date:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *date = [dateFormatter dateFromString:@"2013-06-19 18:10:00"];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(date<%@)",date];
在SQL中作为查询条件通常用WHERE,但在COREDATA中作为查询条件就可以用到NSPredicate.
NSPredicate 不单可以和COREDATA中的FetchRequest 配合使用。也可以与NSArray配合使用。
NSPredicate 中支持的关键词和条件符:
1、>,<,>=,<=,= 比较运算符。
如:
NSPredicate * qcondition= [NSPredicate predicateWithFormat:@"salary >= 10000"];
2、字符串操作(包含):BEGINSWITH、ENDSWITH、CONTAINS
如:
@"employee.name BEGINSWITH[cd] '李'" //姓李的员工
@"employee.name ENDSWITH[c] '梦'" //以梦结束的员工
@"employee.name CONTAINS[d] '宗'" //包含有"宗"字的员工
注:[c]不区分大小写[d]不区分发音符号即没有重音符号[cd]既不区分大小写,也不区分发音符号。
3、范围:IN ,BWTEEN
如:
@"salary BWTEEN {5000,10000}"
@"em_dept IN '开发'"
4、自身:SELF,这个只针对字符数组起作用。
如:
NSArray * test = =[NSArray arrayWithObjects: @"guangzhou", @"beijing", @"shanghai", nil];
@"SELF='beijing'"
5、通配符:LIKE
LIKE 使用?表示一个字符,*表示多个字符,也可以与c、d 连用。
如:
@"car.name LIKE '?he?'" //四个字符中,中间为he
@"car.name LIKE '*jp'" //以jp结束
6、正则表达式:MATCHES
如:
NSString *regex = @"^E.+e$";//以E 开头,以e 结尾的字符。
NSPredicate *pre= [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
if([pre evaluateWithObject: @"Employee"]){
NSLog(@"matches YES");
}else{
NSLog(@"matches NO");
}
7、逻辑运算符:AND、OR、NOT
如:
@"employee.name = 'john' AND employee.age = 28"
8、占位符:
NSPredicate *preTemplate = [NSPredicate predicateWithFormat:@"name==$NAME"];
NSDictionary *dic=[NSDictionary dictionaryWithObjectsAndKeys:
@"Name1", @"NAME",nil];
NSPredicate *pre=[preTemplate predicateWithSubstitutionVariables: dic];
占位符就是字典对象里的key,因此你可以有多个占位符,只要key 不一样就可以了。
query by page
[fetchRequest setFetchLimit:20];
[fetchRequest setFetchOffset: page * 20];
query by date:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *date = [dateFormatter dateFromString:@"2013-06-19 18:10:00"];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(date<%@)",date];
相关文章推荐
- springcache使用笔记002_注释驱动的 Spring cache 按条件查询
- ORACLE——ROWNUM解析(使用ROWNUM大于条件,无法得到任何查询结果)
- 使用准则进行条件查询
- Linq多条件查询优化---使用扩展方法
- EF5.x Code First一对多关联条件查询Contains、Any和All使用示例
- 思达报表工具Style Report基础教程—在查询条件中使用变量
- ofbiz总结——使用performFind查询时如何设置查询条件值为null的情况
- 在Bootstrap开发框架中使用bootstrapTable表格插件和jstree树形列表插件时候,对树列表条件和查询条件的处理
- SQL中多条件查询的处理,避免使用SQL拼接
- 请教一下各位大侠,关于sql语句中使用case替换where条件的查询出现空行的原因
- 使用Python读取TestTrack记录- Part3 动态生成Linq to xml查询语句筛选条件
- 使用反射让linq实现动态查询, 类似拼接sql语句的where 条件
- NSPredicate 条件查询或过虑
- mysql使用别名作为查询条件的两种方式,必有一种适合你
- 使用session在不同页面之间传递参数,sqldatasource按条件查询
- mybatis查询sql中in条件使用(foreach)
- Entity Framework 使用注意:Where查询条件中用到的关联实体不需要Include
- LigerUI中根据表单查询条件进行查询,使用表格Grid展示
- QA16复制_新增查询条件,修改批量使用决策
- MySQL中不允许使用列别名作为查询条件