Question

Code that works:

A) FMResultSet *r = [self.db executeQuery:@"SELECT COUNT(*) FROM myDB"];

What I want to work:

B) r = [self.db executeQuery:@"SELECT COUNT(*) FROM ?",@"myDB"];

The error when using B:

DB Error: 1 "near "?": syntax error"
DB Query: SELECT COUNT(*) FROM ?

How can I get B to work?

Was it helpful?

Solution

I don't think you can pass table name in a parameterised query.

As an alternative you can do it like:

- (void)queryBuilder:(NSString *)tableName
{
   NSString *result = [NSString stringWithFormat:@"SELECT COUNT(*) FROM %@",tableName];
   return result;
}

And use it like:

r = [self.db executeQuery:[self queryBuilder:@"myDB"]];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top