将select语句指定为嵌套查询

时间:2017-12-14 05:38:44

标签: sqlite variables

 String query1 = "SELECT * FROM table1 WHERE name LIKE 'a_'";
 String query2 = "SELECT * FROM ( '" + query1 + "' ) WHERE age = '55'";

我试图将query1作为嵌套选择查询的变量,java中显示的错误是这个...... org.sqlite.SQLiteException:[SQLITE_ERROR] SQL错误或缺少数据库。 如何将query1指定为变量?

1 个答案:

答案 0 :(得分:1)

您的代码query2最终成为

SELECT * FROM ( 'SELECT * FROM table1 WHERE name LIKE 'a_'' ) WHERE age = '55'

如果你改变了

String query2 = "SELECT * FROM ( '" + query1 + "' ) WHERE age = '55'";

String query2 = "SELECT * FROM ( " + query1 + " ) WHERE age = '55'";

(请注意已删除的')结束:

SELECT * FROM ( SELECT * FROM table1 WHERE name LIKE 'a_' ) WHERE age = '55'

这应该是正确的SQLite语法。

我怀疑丢失数据库的错误来自于"混乱" SQLite可以通过修复语法来解决。否则请提供此更改的结果,最好是MCVE的形状。

顺便说一句,我没有得到那个查询的目的,它似乎有点迂回......