Join tables in android

I have 3 tables in my sqlite database. Two configuration tables - one of the main tables where my data is stored. now how to extract data from the main table by joining 2 tables in android, how and where to perform join operations.

Can anyone guide me

+5
source share
3 answers

you can just do rawQuery.
For example, something like this:

db.rawQuery("SELECT a.* 
FROM table_1 a 
INNER JOIN table_2 b ON a.id=b.anyId 
INNER JOIN table_3 c ON b.id= c.anyId
WHERE c.key = ?", new String[]{"test"});

The first parameter is the query you want to execute. For all of your keys that you want to add to your request, just add ?to the request.
The second parameter is String Array. In this array you put your keys, as an example of the above values test.

EDIT:

rawQuery update, insert delete.
, :

db.rawQuery("UPDATE table_1
SET fieldA = ?,
fieldB = ?
WHERE id = ?", new String[]{"test", "test2", "1"});
+9

DatabaseHelper SQLiteOpenHelper

...

    SQLiteDatabase db = this.getReadableDatabase();

    private final String MY_QUERY = "YOUR QUERY";

    db.rawQuery(MY_QUERY, new String[]{"Parameter"});
    db.close();
0

You need rawQuery to solve this problem.

private final String MY_QUERY = "SELECT * FROM table_a a INNER JOIN table_b b ON a.id=b.other_id WHERE b.property_id=?";

db.rawQuery(MY_QUERY, new String[]{String.valueOf(propertyId)});

But if you want to make a good implementation, create an SQLiteHelper and a DataSource for each table and do the relationship in the data source.

-1
source

All Articles