问题描述
我创建不同主题的Android应用程序测验。所以,我对主题的按钮和不同的表在我的数据库为Quiz1,Quiz2,Quiz3等等...
I am creating an android app quiz with different topics. So i have buttons for the topics and different TABLEs in my database for Quiz1, Quiz2, Quiz3 and so on...
下面是我的code为Quiz.java
Here's my code for Quiz.java
public class Quiz extends Activity implements OnClickListener{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.quiz);
Button quiz1 = (Button) findViewById(R.id.quiz1Btn);
quiz1.setOnClickListener(this);
Button quiz2 = (Button) findViewById(R.id.quiz2Btn);
quiz2.setOnClickListener(this);
}
@Override
public void onClick(View v) {
Intent i;
switch (v.getId()){
case R.id.quiz1Btn :
//Get Question set
List<Question> questions = getQuestionSetFromDb();
//Initialize quiz with retrieved question set ///
CurrentQuiz c = new CurrentQuiz();
c.setQuestions(questions);
c.setNumRounds(getNumQuestions());
((MLearningApp)getApplication()).setCurrentGame(c);
i = new Intent(this, QuestionActivity.class);
startActivity(i);
break;
}
}
// Method that retrieves a random set of questions from db
private List<Question> getQuestionSetFromDb() throws Error {
int numQuestions = getNumQuestions();
DBHelper myDbHelper = new DBHelper(this);
try {
myDbHelper.createDataBase();
} catch (IOException ioe) {
throw new Error("Unable to create database");
}
try {
myDbHelper.openDataBase();
}catch(SQLException sqle){
throw sqle;
}
List<Question> questions = myDbHelper.getQuestionSet(numQuestions);
myDbHelper.close();
return questions;
}
// Method to return the number of questions for the game
private int getNumQuestions() {
int numRounds = 10;
return numRounds;
}
这是我的查询中DBHelper.java
And here's my query in DBHelper.java
public List<Question> getQuestionSet(int numQ){
List<Question> questionSet = new ArrayList<Question>();
Cursor c = myDataBase.rawQuery("SELECT * FROM Quiz1" + " ORDER BY RANDOM() LIMIT " + numQ, null);
while (c.moveToNext()){
//Log.d("QUESTION", "Question Found in DB: " + c.getString(1));
Question q = new Question();
q.setQuestion(c.getString(1));
q.setAnswer(c.getString(2));
q.setOption1(c.getString(3));
q.setOption2(c.getString(4));
questionSet.add(q);
}
return questionSet;
}
我的问题是,我怎么会在我的数据库为Quiz2按钮选择Quiz2表;和相同分贝为Quiz3按钮Quiz3表等。
My question is, how would i select Quiz2 TABLE in my database for Quiz2 button; and same as Quiz3 TABLE in db for Quiz3 button and so on.
到目前为止,我的Android应用正在为Quiz1只因为它仅检索我的数据库一个表这对Quiz1。
So far, my android app is working for Quiz1 only since it only retrieves one table from my database which is for the Quiz1.
推荐答案
您必须将表名称更改为 Quiz2
等。
这要求功能需要知道测验数:
You have to change the table name to Quiz2
, etc.This requires that the function needs to know the quiz number:
public List<Question> getQuestionSet(int quizNr, int numQ) {
...
rawQuery("SELECT * FROM Quiz" + quizNr + " ORDER BY random() LIMIT " + numQ, null);
...
}
如果你有一个表,你就必须在相应的列进行过滤:
If you had a single table, you would have to filter on the corresponding column:
public List<Question> getQuestionSet(int quizNr, int numQ) {
...
rawQuery("SELECT * FROM Quiz WHERE QuizNr = " + quizNr + " ORDER BY random() LIMIT " + numQ, null);
...
}
这篇关于SQLite数据库查询多个表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!