Search code examples
sqliteandroid-sqliterandom-data

Saving random rows from a sqlite database to another temporary sqlite database


I choose 20 random rows from my sqlite database like this:

String selectQuery = "SELECT * FROM " + TABLE_QUESTION + " WHERE " + KEY_ID+ " IN (SELECT "+ KEY_ID + " FROM " +TABLE_QUESTION+" ORDER BY RANDOM() LIMIT 20)";

I display them in Activity A. And after some time I go from Activity A to Activity B and finally Activity C.

The same random rows that is displayed in Activity A should be displayed with the same order in Activity C too.

For this, do I have to save the random rows to another temporary sqlite database that is always cleared up after Activity C, if so, how?

Any help would be appreciated.


Solution

  • You can query the same random set multiple times from a temporary table like this:

    create temporary table tmp AS select id from tableQ ORDER BY random() LIMIT 20;
    Select id from tmp;
    Select id from tmp;
    drop table tmp