0

I have a database imported in assets folder. When I call nextQuestion() method I need to retrieve first entry from database. After user answer, I need to load nextQuestion() method again and retrieve next entry from database. I tried this but it did not work, I always get only first entry:

String sql ="SELECT * FROM tblTable ORDER BY _ID ASC LIMIT 1";

Also tried this:

String sql ="SELECT * FROM tblTable ORDER BY _ID ASC";

5 Answers 5

0

Of course you always get only the first one, you execute the same query over and over again. If the data is not enormous, use your second query. Execute it just once and store all rows in the memory. Your nextQuestion() method does then not need to do a DB query anymore.

2
  • How to do that? I tried that, in my adapter class, and then in my nextQuestion I open db and use a cursor. But always get first row.
    – marjanbaz
    Apr 16, 2013 at 1:01
  • Do not execute the query every time! Execute it once and get all the rows at once. Google for examples. Here is one: vogella.com/articles/AndroidSQLite/article.html see for example the method getAllComments()
    – SimonSays
    Apr 16, 2013 at 3:38
0

I believe you need to use your second query once, capture the Resultset and then either traverse it directly using first, next, previous, etc or load it in one loop into ArrayList and use its methods.

0

Though not efficient, you can use the following query to get the next question:

SELECT * 
FROM 
(
    SELECT * 
    FROM 
        questions 
    ORDER BY ID ASC LIMIT :questionNumber
) ORDER BY ID DESC LIMIT 1

where :questionNumber is the nth question that is of interest.

E.g: http://sqlfiddle.com/#!7/a20b8/4

3
  • Insead of this, is it a good idea to store asked question, using cursor, and then when calling nextQuestion method to check asked questions, and ask the first one not asked?
    – marjanbaz
    Apr 16, 2013 at 1:06
  • To answer myself...it's not good idea. :) Although the idea is good, it's not working. I've just tried it.
    – marjanbaz
    Apr 16, 2013 at 1:09
  • Cursors are not generally encouraged in production environment, @marjanbaz.
    – Vikdor
    Apr 16, 2013 at 4:17
0

What you actually want is the question with the smallest ID that is still larger than your previous ID:

SELECT *
FROM tblTable
WHERE _ID > ?    -- insert previous ID here
ORDER BY _ID
LIMIT 1
0

-> Execute String sql ="SELECT * FROM tblTable ORDER BY _ID ASC"; just one time,
-> Keep datas in an ArrayCollection
-> create global varible, whichQuestion:Number; // keep which question you are in
-> create a method like

  nextQuestion(whichQuestion:Number):String //pass which question you want to ask
{
  return questionCollection[i];
} 

-> then whichQuestion +=1;
-> finally , simply call nextQuestion() method when ever you need question

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.