Search code examples
androidsqliteloader

SQLite Android load rows based on ids, obtained from a string array in a different table?


I have a database table A, that contains two columns: the primary key _idA and a column called datalist. I also have a database table B, containing multiple columns, one of them being its primary key _idB, the others contain misc data.

I want to load the datalist from table A by a specific _idA. This datalist is a string, containing _idBs separated by a comma, i.e. "31,62,612,6123,682". After that, I need to load all rows from table B, that have an _idB that is contained in the datalist of table A.

Since I am on Android, working with a Dataloader, that is connected to a Listview, if possible I would really like to do all this in one query, since I am not completely sure it is possible to do two separate queries in one Android DataLoader.

Example: -> My program needs all items with an id that is stored in the datalist of _idA = 5 -> Get datalist from table A, where _idA = 5 -> Get all rows from table B, where _idB is in datalist from table A, _idA = 5

I am still pretty new to SQLite and still learning, so thanks a lot for your help!


Solution

  • Assuming that by DataLoader you are referring to the CursorLoader, things are simple:

    You should either:

    1. Extent a cursor loader and in onLoadInBackground() method do the first table query, get the id and use it in the second table query. Make sure to return the Cursor and swap it in the ListView adapter.

    2. In the first cursor loader onLoadFinished() callback, get the id and use it to start a new loader for the second table cursor.

    Let me know if you need more help.