Question

Is it possible to bind a parameter in the FROM clause of a query to SQLite? How?

If not, why? Any alternatives?

Here is what I want to do in my iOS application:

- (BOOL)existsColumn:(NSString *)column inTable:(NSString *)table ofDatabase:(FMDatabase *)database {
    NSString *query = @"SELECT ? FROM ? LIMIT 0";
    NSArray *queryParameters = @[column, table];
    return (BOOL)[database executeQuery:query withArgumentsInArray:queryParameters];
}

Here is the error message I get:

near "?": syntax error

I understand that there are other ways to check if a column exists. I am specifically curious why I can't bind a parameter in the FROM clause (only binding a parameter in the SELECT clause above works so it must be an issue with the FROM clause).

Was it helpful?

Solution

The ? should only be used for binding actual values into the query such in the VALUES section of an INSERT or values in a WHERE clause.

For dynamically building the column and table names you should simply use a string format to build the query string before sending it to FMDB.

- (BOOL)existsColumn:(NSString *)column inTable:(NSString *)table ofDatabase:(FMDatabase *)database {
    NSString *query = [NSString stringWithFormat:@"SELECT %@ FROM %@ LIMIT 0", column, table];
    return (BOOL)[database executeQuery:query];
}

Please note that you should not use the string format in place of using ? and proper value binding for actual values (which you don't have in this case).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top