Amer A.
Amer A.

Reputation: 1045

How do I join two SQLite tables with the Android API?

I like to use the query method instead of rawQuery because it is more elegant for me, but it seems that the method can receive only one table as an input parameter.

Is there a solution for many tables?

Upvotes: 0

Views: 682

Answers (2)

waqaslam
waqaslam

Reputation: 68187

I dont think there's any other method which can offer you this flexibility. Thats why the API comes with the method rawQuery(). Use sql query using UNION to join data from two tables.

for example:

String query = "SELECT Id, Name FROM Finance " + 
               "UNION ALL " +
               "SELECT Id, Name FROM Marketing";

Cursor c = db.rawQuery(query, null);

Upvotes: 1

Dre
Dre

Reputation: 4329

Best will be just using rawquery, but if you must stick with query:

( please note, I have not tested this, but I imagine it will work )

You should be able to create a view ( http://sqlite.org/lang_createview.html ) and then use the view name in place of a table name.

Upvotes: 0

Related Questions