0

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.

Hasan Saykın
  • 138
  • 1
  • 8
  • in theory, you would just need the 20 IDs. – CL. Oct 26 '17 at 11:33
  • I have a quiz app. In Activity A, I get 20 random questions from questions.db with the help of a model class, and display them one by one. So I'm thinking of saving every question info orderly to another temporary database which will reset everytime after using it in Activity C. @CL. – Hasan Saykın Oct 26 '17 at 17:38

1 Answers1

1

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
itsLex
  • 786
  • 1
  • 5
  • 13
  • I'm closing database connection after Activity A is stopped. And I create a new instance of the database class in Activity C. Will this temp table still be there after all this? – Hasan Saykın Oct 29 '17 at 19:09
  • 1
    No, the temporary table will be gone. In that case you'd better create a real table, and drop it right before you create it. – itsLex Oct 29 '17 at 19:38
  • Thank you, I will try it and let you know. @itsLex – Hasan Saykın Oct 31 '17 at 17:34
  • I now save random questions, one by one as they show up in Activity A, to another similar database thorugh an insert method I added. And I added a drop and recreate method which helps me reset all the data in it everytime Activity A is created. So my problem is solved. @itsLex – Hasan Saykın Nov 04 '17 at 11:08