Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to join tables using sqlite in android

Tags:

I am trying to find out how to do a simple table join on my two tables using a sqlite database in an android application.

Is the simplest way to use CursorJoiner or is there any easier way?

like image 868
Dale Marshall Avatar asked Jul 19 '10 01:07

Dale Marshall


People also ask

How can I join more than two tables in SQLite?

To query data from multiple tables, you use INNER JOIN clause. The INNER JOIN clause combines columns from correlated tables. Suppose you have two tables: A and B. A has a1, a2, and f columns.

Which join is supported in SQLite?

The OUTER JOIN Though SQL standard defines three types of OUTER JOINs: LEFT, RIGHT, and FULL, SQLite only supports the LEFT OUTER JOIN.


2 Answers

In the implementation of SQLiteDatabase and SQLiteQueryBuilder you will see that it is possible to pass the tables you want to join to the table argument of query even though the documentation implies it will only take a single name of a table. The documentation for SQLiteQueryBuilder is more clear and even suggests things like foo, bar or foo LEFT OUTER JOIN bar ON (foo.id = bar.foo_id).

like image 150
Tim Kryger Avatar answered Oct 19 '22 08:10

Tim Kryger


If you're willing to use a third-party library, I recommend using Active Android

https://github.com/pardom/ActiveAndroid

using it you can merge tables like this

From query = new Select()     .from(Foo.class)     .innerJoin(Bar.class)     .on("Foo.Id=Bar.Id"); 

you can even get a cursor back to use in loaders

Cursor cursor = Cache.openDatabase().rawQuery(query.toSql(), query.getArguments()); 

more details here

like image 25
bigsolom Avatar answered Oct 19 '22 10:10

bigsolom