I’m creating an app on android studio.I randomly get a question using getQuestion cursor first where id = 0(will change this to something else later)
SQLiteDatabase myDatabase = this.openOrCreateDatabase("Questions", MODE_PRIVATE, null); Cursor getQuestion = myDatabase.rawQuery("SELECT question FROM questions WHERE id = '0' ORDER BY RANDOM() LIMIT 1",null); int questionIndex = getQuestion.getColumnIndex("question");
In the same table there is answer column.I would like to get the answer of the question which i randomly got from getQuestion query using something like this
Cursor getAnswer = myDatabase.rawQuery("SELECT answer FROM questions WHERE question = ?");
I couldnt find how to use the question as a string and use it in the getAnswer query.What should i do ?
Just read both columns with the same query:
Cursor getQuestion = myDatabase.rawQuery("SELECT question, answer FROM ..."); int questionIndex = getQuestion.getColumnIndexOrThrow("question"); int answerIndex = getQuestion.getColumnIndexOrThrow("answer"); ...
you can simply do:
Cursor getAnswer = myDatabase.rawQuery("SELECT answer FROM questions WHERE question = " + questionIndex +";");