2017-07-28 65 views
0

我創建於Android studio.I應用程序首先使用getQuestion光標隨機得到一個問題,其中id = 0(將別人改變這一點的東西)如何在rawquery中使用字符串?

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"); 

在同一個桌子上有答案列。我想獲得我隨機getQuestion查詢中使用這樣的事情

Cursor getAnswer = myDatabase.rawQuery("SELECT answer FROM questions WHERE question = ?"); 

我無法找到如何使用的問題作爲一個字符串,並在的getAnswer query.What使用它,我應該得到了問題的答案做什麼?

回答

0

具有相同的查詢剛看完兩列:

Cursor getQuestion = myDatabase.rawQuery("SELECT question, answer FROM ..."); 
int questionIndex = getQuestion.getColumnIndexOrThrow("question"); 
int answerIndex = getQuestion.getColumnIndexOrThrow("answer"); 
... 
+0

謝謝你這麼多,解決我的問題。 – MePengusta

0

,你可以簡單地做:

Cursor getAnswer = myDatabase.rawQuery("SELECT answer FROM questions WHERE question = " + questionIndex +";"); 
相關問題