uff da
uff da

Reputation: 597

What does Unable to generate SQL for predicate (_STRING PREDICATE_) (problem on RHS) mean?

I'm trying to develop an app for iPhone using Core Data and I'm using a very basic predicate to grab objects. (i.e. attribute_name==string)

I've checked the model to make sure the attribute names are correct and I know from this SO question that RHS means "Right Hand Side," which leads me to the "string" part. I'm creating the format string using

NSString *predicateString = [NSString stringWithFormat:@"attribute_name==%@", string];

Then to fetch, I'm using this:

NSEntityDescription *entityDescription = [NSEntityDescription entityForName:_entityName inManagedObjectContext:managedObjectContext];
NSFetchRequest *request = [[[NSFetchRequest alloc] init] autorelease];
[request setEntity:entityDescription];
NSPredicate *predicate = [NSPredicate predicateWithFormat:predicateString];
[request setPredicate:predicate];
NSError *error = nil;
NSArray *array = [managedObjectContext executeFetchRequest:request error:&error];

What am I missing? What could cause this error to occur?

Thanks.

Upvotes: 18

Views: 7066

Answers (2)

Brent Priddy
Brent Priddy

Reputation: 3847

You need to quote the string if you really want to use the NSString stringWithFormat: message or you could use the NSPredicate predicateWithFormat: message with the formatting and arguments because it will "do the right thing" with the rhs in your situation (I would suggest that you use the NSPredicate method without the quotes)

NSString *predicateString = [NSString stringWithFormat:@"attribute_name==\"%@\"", string];

Upvotes: 42

Dave DeLong
Dave DeLong

Reputation: 243156

You're doing this wrong. You should not be creating a format string.

I'm guessing (based on your reaction to @Brent's answer) that you're doing something like this:

NSString *predicateString = [NSString stringWithFormat:@"foo == \"%@\"", bar];
NSPredicate *p = [NSPredicate predicateWithFormat:predicateString];

Don't do that.

Do this instead:

NSPredicate *p = [NSPredicate predicateWithFormat:@"foo == %@", bar];

The first will fail if bar happens to contain a " character. The second will not.

Upvotes: 19

Related Questions