我的CoreData中的实体名为myentity
,属性名为myuniqueid
,我想按如下方式运行SQL查询:
Select * from myentity where myuniqueid == 1000
如何在SQL查询中获取指定的匹配行?这样我想过滤myuniqueid
为1000
的所有行。
我的代码:
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSManagedObjectContext *context = [self managedObjectContext];
if (context == nil) {}
else {
NSEntityDescription *entity = [NSEntityDescription entityForName:@"myentity" inManagedObjectContext:context];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"myuniqueid == 1000"];
[fetchRequest setPredicate:predicate];
[fetchRequest setEntity:entity];
NSArray *items = [context executeFetchRequest:fetchRequest error:nil];
for (int i=0; i<4; i++) {
NSManagedObject *mo = [items objectAtIndex:i]; // assuming that array is not empty
id value = [mo valueForKey:@"myuniqueid"];
NSLog(@"SENDER: %@",value);
}
}
我收到错误:'Unable to generate SQL for predicate (myuniqueid == 1000) (problem on RHS)'
答案 0 :(得分:1)
试试这个谓词:
NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"myentity"];
fetchRequest.predicate = [NSPredicate predicateWithFormat:@"myuniqueid == %@", @(1000)];
NSArray *items = [[self managedObjectContext] executeFetchRequest:request error:nil];