Khushbu Shah
Khushbu Shah

Reputation: 1683

Database query android sqlite

I am stuck with one scenario in which I need to do complex query to get cursor. Don't know It is possible or not. Scenario is : There are three tables in database.

 Table         Columns  
Table-1    _id, name, number, ....  
Table-2    _id, table1_id, col1, col2, ....  
Table-3    _id, table2_id, col1, col2, ....

In these tables, when any record is inserted in table 2, corresponding table1 id is inserted in that record. Same for table2 id is inserted with table 3 record.

I want cursor for CursorAdapter to display list view of Table-3 data.

Cursor cursor = getContentResolver().query(TABLE_3_NAME, null, null, null, null);

Now need to add selection and selection args of Table-1 in this query.
Is it possible in Android?

Upvotes: 1

Views: 129

Answers (2)

Paresh Mayani
Paresh Mayani

Reputation: 128428

You can write a SQL selection query (i.e. normal way) and you can execute using rawQuery() method.

For example:

Cursor cursor = db.rawQuery(selectQuery, null);

Upvotes: 1

Ragesh Ramesh
Ragesh Ramesh

Reputation: 3520

You can use raw query to fetch from various tables. One example would be as below.

 Cursor mCursor = db.rawQuery("SELECT * FROM Table-1, Table-3 " +
                         "WHERE Table1.id = <Whatever selection args you want> " +
                         "GROUP BY Table1.id", null);

This is just an example. You will have to change it as per your requirement.

Upvotes: 1

Related Questions