Fmdb Select 带有可变表名的查询 [英] Fmdb Select query with variable table name

查看:37
本文介绍了Fmdb Select 带有可变表名的查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从不同的表中获取结果,表是由用户选择的.所以,我使用表名作为变量,但它返回 nil 查询.

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'"];

我记录了 tableName 值,它返回了正确的值,即Class".请帮我解决这个问题,并为此建议最好的查询.

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.

推荐答案

使用此代码:

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]]];

这篇关于Fmdb Select 带有可变表名的查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆