使用CoreData查询数据:谓词过滤,排序操作
2015-05-21 20:22
429 查看
- (void)_query
{
// 初始化查询请求
// NSFetchRequest * request = [[NSFetchRequest alloc] initWithEntityName:@"Student"];
// 调用的是上下文的方法
// 方法一
// 通过父类的KVC方式查询
// NSArray * array = [self.context executeFetchRequest:request error:nil];
// for (NSManagedObject * student in array)
// {
// NSLog(@"%@--- %i", [student valueForKey:@"name"], [[student valueForKey:@"age"] intValue]);
// }
// 方法二
// 通过映射对象获取(常用)
// NSArray * array = [self.context executeFetchRequest:request error:nil];
// for (Student * student in array)
// {
// NSLog(@"%@--- %i", student.name, [student.age intValue]);
// }
//// 方法三
//// 通过子类的KVC方式查询
// NSArray * array = [self.context executeFetchRequest:request error:nil];
// for (Student * student in array)
// {
// NSLog(@"%@--- %i", [student valueForKey:@"name"], [[student valueForKey:@"age"] intValue]);
// }
// 谓词过滤
// 过滤其他查询
// 1、<,>,=
//// NSPredicate * predict = [NSPredicate predicateWithFormat:@"name = lisi"];//错误写法
//// NSPredicate * predict = [NSPredicate predicateWithFormat:@"name = %@", @"lisi"];//正确写法
// NSPredicate * predict = [NSPredicate predicateWithFormat:@"name = 'lisi'"];//正确写法
// request.predicate = predict;//不赋值就是对全部内容操作
// 2.like
// NSPredicate * predict = [NSPredicate predicateWithFormat:@"name like %@", @"*lisi*"];//注意使用的是*
// request.predicate = predict;
// 3.and,or
// NSPredicate * predict = [NSPredicate predicateWithFormat:@"name = 'lisi' and age = 56"];
// NSPredicate * predict = [NSPredicate predicateWithFormat:@"name = %@ and age = %i", @"lisi", 56];
// NSPredicate * predict = [NSPredicate predicateWithFormat:@"name = %@ && age = %i", @"lisi", 56];
// request.predicate = predict;
// 4.in
// NSPredicate * predict = [NSPredicate predicateWithFormat:@"name in%@", @[@"lisi", @"zhangsanlisi"]];
// request.predicate = predict;
// 5.between
// 使用字符串的方式
// NSPredicate * predict = [NSPredicate predicateWithFormat:@"age between {0,120}"];
// NSPredicate * predict = [NSPredicate predicateWithFormat:@"age BETWEEN {0,120}"];
// 或者使用数组的方式
// NSPredicate * predict = [NSPredicate predicateWithFormat:@"age between %@", @[@22, @24]];//两个数倒过来就查不出来了
// predict = [NSPredicate predicateWithFormat:predict.predicateFormat];
// NSLog(@"%@", predict.predicateFormat);//打印predict的字符串
// request.predicate = predict;
//
// 排序
// NSSortDescriptor * sort = [NSSortDescriptor sortDescriptorWithKey:@"age" ascending:NO];
// request.sortDescriptors = @[sort];//数组中可以放置多个sort,一般就用一个
// 打印返回的结果
// NSArray * array = [self.context executeFetchRequest:request error:nil];
// for (Student * student in array) {
// NSLog(@"%@-- %i", student.name, [student.age intValue]);
// }
}
{
// 初始化查询请求
// NSFetchRequest * request = [[NSFetchRequest alloc] initWithEntityName:@"Student"];
// 调用的是上下文的方法
// 方法一
// 通过父类的KVC方式查询
// NSArray * array = [self.context executeFetchRequest:request error:nil];
// for (NSManagedObject * student in array)
// {
// NSLog(@"%@--- %i", [student valueForKey:@"name"], [[student valueForKey:@"age"] intValue]);
// }
// 方法二
// 通过映射对象获取(常用)
// NSArray * array = [self.context executeFetchRequest:request error:nil];
// for (Student * student in array)
// {
// NSLog(@"%@--- %i", student.name, [student.age intValue]);
// }
//// 方法三
//// 通过子类的KVC方式查询
// NSArray * array = [self.context executeFetchRequest:request error:nil];
// for (Student * student in array)
// {
// NSLog(@"%@--- %i", [student valueForKey:@"name"], [[student valueForKey:@"age"] intValue]);
// }
// 谓词过滤
// 过滤其他查询
// 1、<,>,=
//// NSPredicate * predict = [NSPredicate predicateWithFormat:@"name = lisi"];//错误写法
//// NSPredicate * predict = [NSPredicate predicateWithFormat:@"name = %@", @"lisi"];//正确写法
// NSPredicate * predict = [NSPredicate predicateWithFormat:@"name = 'lisi'"];//正确写法
// request.predicate = predict;//不赋值就是对全部内容操作
// 2.like
// NSPredicate * predict = [NSPredicate predicateWithFormat:@"name like %@", @"*lisi*"];//注意使用的是*
// request.predicate = predict;
// 3.and,or
// NSPredicate * predict = [NSPredicate predicateWithFormat:@"name = 'lisi' and age = 56"];
// NSPredicate * predict = [NSPredicate predicateWithFormat:@"name = %@ and age = %i", @"lisi", 56];
// NSPredicate * predict = [NSPredicate predicateWithFormat:@"name = %@ && age = %i", @"lisi", 56];
// request.predicate = predict;
// 4.in
// NSPredicate * predict = [NSPredicate predicateWithFormat:@"name in%@", @[@"lisi", @"zhangsanlisi"]];
// request.predicate = predict;
// 5.between
// 使用字符串的方式
// NSPredicate * predict = [NSPredicate predicateWithFormat:@"age between {0,120}"];
// NSPredicate * predict = [NSPredicate predicateWithFormat:@"age BETWEEN {0,120}"];
// 或者使用数组的方式
// NSPredicate * predict = [NSPredicate predicateWithFormat:@"age between %@", @[@22, @24]];//两个数倒过来就查不出来了
// predict = [NSPredicate predicateWithFormat:predict.predicateFormat];
// NSLog(@"%@", predict.predicateFormat);//打印predict的字符串
// request.predicate = predict;
//
// 排序
// NSSortDescriptor * sort = [NSSortDescriptor sortDescriptorWithKey:@"age" ascending:NO];
// request.sortDescriptors = @[sort];//数组中可以放置多个sort,一般就用一个
// 打印返回的结果
// NSArray * array = [self.context executeFetchRequest:request error:nil];
// for (Student * student in array) {
// NSLog(@"%@-- %i", student.name, [student.age intValue]);
// }
}
相关文章推荐
- 使用CoreData查询数据:谓词过滤,排序操作
- sparkSQL里 sql语句,dataframe,Thrift Server JDBC都可以实现对数据的查询,过滤等操作, 哪这3种情况分别是什么情况下使用
- play framework框架下,连接到MongoDB时的特殊操作(与或查询,模糊查询,范围查询,数据排序等)
- 数据查询使用数字代替字段排序
- ORACLE使用row_number() over(...)对查询数据进行分组并排序
- oracle 使用instr()函数对in查询出的记录按照in中的数据进行排序
- 项目中使用的自定义数据访问类(复杂查询,复杂操作)
- 项目中使用的自定义数据访问类(复杂查询,复杂操作)
- JPA数据的操作:增加,删除,修改,获取,使用JPOL进行查询
- .NET 使用GridView控件绑定数据源其他操作(查询相关数据,而不是显示全数据)
- MFC中使用sqlite3操作数据库 创建,插入数据,查询数据
- 数据查询使用数字代替字段排序
- 使用bootstraptable插件实现表格记录的查询、分页、排序操作
- java操作elasticsearch使用QueryBuilders进行数据查询
- 【转】数据库基本知识:(十)数据操作 · 查 · (三)使用子查询访问和修改数据
- 使用ADO.NET查询和操作数据
- 转自:http://m.blog.csdn.net/article/details?id=6554168 在使用order by语句进行查询结果排序时,不同的数据库对于被排序字段数据行为null的情况
- MySQL使用 IN 查询取出数据排序问题(与in排序相同、不排序)
- DataTables+BootStrap组合使用Ajax来获取数据并且动态加载dom的方法(排序,过滤,分页等)
- DataTables+BootStrap组合Ajax数据使用方法(排序,过滤,分页等)