Question

I want to get results from different tables, tables are selected by the user. So, I am using table name as variable but it returns nil query.

FMResultSet *query = [db1 executeQuery:@"SELECT Image, Explanation FROM %@ WHERE Image !='empty'"
"UNION SELECT Image, Explanation FROM %@ WHERE Image !='empty'"
"UNION SELECT Image, Explanation FROM %@ WHERE Image !='empty'"
"UNION SELECT Image, Explanation FROM %@ WHERE Image !='empty'"
"UNION SELECT Image, Explanation FROM %@ WHERE Image !='empty'"
"UNION SELECT Image, Explanation FROM %@ WHERE Image !='empty'", [NSString stringWithFormat:@"%@1",tableName], [NSString stringWithFormat:@"%@2",tableName],
[NSString stringWithFormat:@"%@3",tableName], [NSString stringWithFormat:@"%@4",tableName], [NSString stringWithFormat:@"%@5",tableName], [NSString stringWithFormat:@"%@6",tableName]];

If I hardcode the table name it returns the data.

FMResultSet *query = [db1 executeQuery:
@"SELECT Image, Explanation FROM Class1 WHERE Image !='empty'"
"UNION SELECT Image, Explanation FROM Class2 WHERE Image !='empty'"
"UNION SELECT Image, Explanation FROM Class3 WHERE Image !='empty'"
"UNION SELECT Image, Explanation FROM Class4 WHERE Image !='empty'"
"UNION SELECT Image, Explanation FROM Class5 WHERE Image !='empty'"
"UNION SELECT Image, Explanation FROM Class6 WHERE Image !='empty'"];

I logged the tableName value and it is returning the correct value, which is "Class". Please help me solving this issue and also suggest the best query for this purpose.

Was it helpful?

Solution

Use this Code :

FMResultSet *query = [db1 executeQuery:[NSString stringWithFormat:@"SELECT Image, Explanation FROM %@ WHERE Image !='empty'"
"UNION SELECT Image, Explanation FROM %@ WHERE Image !='empty'"
"UNION SELECT Image, Explanation FROM %@ WHERE Image !='empty'"
"UNION SELECT Image, Explanation FROM %@ WHERE Image !='empty'"
"UNION SELECT Image, Explanation FROM %@ WHERE Image !='empty'"
"UNION SELECT Image, Explanation FROM %@ WHERE Image !='empty'", [NSString stringWithFormat:@"%@1",tableName], [NSString stringWithFormat:@"%@2",tableName],
[NSString stringWithFormat:@"%@3",tableName], [NSString stringWithFormat:@"%@4",tableName], [NSString stringWithFormat:@"%@5",tableName], [NSString stringWithFormat:@"%@6",tableName]]];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top