views:

26

answers:

1

I wish to execute the following query on my application's SQliteDatabase.

String sql = "Select col_1,row_id from Table1 where row_id IN(Select
row_id from Table2 where key = 'key')";

I am using the rawQuery function of the SQLiteDataBase class for the same. The problem is when the inner query returns 0 objects the query runs and returns 0 results. But when the inner query returns a Set, the query is not executed.

Can we have nested queries like these in rawQuery? What is it that I am missing here?

Please help.

Thanks.

+1  A: 

Why don't you use a join?

SELECT Table1.col_1, Table1.row_id FROM Table1 JOIN Table2 ON Table1.row_id = Table2.row_id WHERE key = 'key';
halfdan
I am not very versed with databases so please excuse my ignorance. I have two questions:1. Are JOINs better than nested queries in terms of space and memory?2. Any idea why the above mentioned query doesn't work on Android? i tried it on the SQlite Database browser and it works.
Samuh
Databases are usually optimized to run JOINs. Internally most query optimizers will transform your nested query into a join. Without any error message I can't tell why it doesn't work when the subquery returns a set. The syntax is correct!
halfdan
+10. thanks for you answer!
Samuh