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

i have tried to put query1 as a variable for the nested select query, the error shown in java is this... org.sqlite.SQLiteException: [SQLITE_ERROR] SQL error or missing database. How to assign query1 as variable?

3
  • It does not look like the result of the first query is ending up in the string variable query1. You probably need to first execute that query and then use the (hopefully single-word) result in the concatenation you seem to attempt in the second query. Commented Dec 14, 2017 at 8:26
  • Please make a minimal reproducible example. Seeing how you use those string variables will probably help with answering your question. Commented Dec 14, 2017 at 8:28
  • query2 ends up being SELECT * FROM ( 'SELECT * FROM table1 WHERE name LIKE 'a_'' ) WHERE age = '55' doesn't it? The 'a_'' looks fishy. Commented Dec 14, 2017 at 8:31

1 Answer 1

1

With your code query2 ends up being

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

If you change

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

to

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

(note the deleted ') it ends up:

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

which should be correct SQLite syntax.

I suspect that error on missing database is coming from a "confused" SQLite and could be solved by fixing the syntax. Otherwise please provide the result of this change, preferrably in the shape of a MCVE.

I do not get the purpose of that query by the way, it seems somewhat roundabout...

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.