faz
faz

Reputation: 453

Retrieving random data from SQLite

I want to design a language learning Android application where random set of questions are retrieved from the database and displayed to the user.

I have different type of questions such as multiple choice questions, fill in the blanks, matching the correct choice, arranging the words into a proper sentence and listening to audio and answering the comprehension questions.

How to store these different types of questions in the database and retrieved randomly and displayed to the user to be answered?

I am using SQLite.

Upvotes: 0

Views: 211

Answers (3)

Chintan Soni
Chintan Soni

Reputation: 25267

Your question is too abstract, still trying to answer your question.

First of all, you will need to create a database which will contain a table that uniquely identifies your questions.

And then, you can fire the query as:

SELECT * FROM table ORDER BY RANDOM() LIMIT 1; which can be used for selecting a random row from sqlite table.

And then, simply code for displaying that question.

Upvotes: 0

ashes999
ashes999

Reputation: 10163

This is a pretty broad question and there are a lot of things you need to consider. For example, the modelling of your questions, and persistence -- storing them in the DB. I made a similar Android app a few years ago.

The first thing I suggest is working in incremental steps; things are much easier if you start out with, say, multiple-choice questions only (that all have four answers). Then once you have that working, add other types of questions.

For modelling your object domain, I suggest starting with a single base Question class. This can contain some common properties, like the unique ID, the name/shortname, perhaps some sort of difficulty metric.

After this, you can have sub-types of questions, such as a MultipleChoiceQuestion, a ShortAnswerQuestion, and an EssayQuestion. They may have other properties (like the list of possible answers for mutliple choice).

Mapping all this to a bunch of tables is not trivial. The usual method is one table per class; another option is one table with all the properties for all classes, and some sort of 'type' discriminator. I suggest the former, since it's a cleaner separation of attributes.

Finally, in terms of random, there are two approaches: random on the database level (more efficient, since it returns less data) or random on the application level. The difference is "hey database, get me N random questions" vs. "hey database, get me all the questions; hey application, pick N random questions."

To leverage the DB, you can use this answer here, where they use ORDER BY RANDOM() LIMIT N. I don't know about SQLite, but in SQL Server, with large data sets, this can be extremely slow; if you have a lot of questions, profile it and see if this is too slow for your app.

If you have a base Questionstable with all questions, this will work quite easily for you. If not, you may need to do something similar where you poll forAmultiple choice questions,Bshort answer questions, etc. whereA + B + ... = N` number of questions you want.

Upvotes: 2

Thom
Thom

Reputation: 15052

The approach I use for this type of problem is to uniquely identify each of your rows. A sequence ID is fine.

Each time you go through the loop, place all of the IDs into an array structure. Each entry in the array has a random number and an ID into your SQLite table.

After you do this, sort the array and now you have randomly sequenced the questions. Process in order.

Upvotes: 0

Related Questions