我在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)'
最佳答案
试试这个谓词:
NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"myentity"];
fetchRequest.predicate = [NSPredicate predicateWithFormat:@"myuniqueid == %@", @(1000)];
NSArray *items = [[self managedObjectContext] executeFetchRequest:request error:nil];