【问题标题】:SQLite Database query for multiple tableSQLite数据库查询多个表
【发布时间】:2014-08-08 16:27:50
【问题描述】:

我正在创建一个包含不同主题的 Android 应用测验。所以我在我的数据库中为 Quiz1、Quiz2、Quiz3 等提供了主题和不同表格的按钮......

这是我的 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 中的查询

    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 TABLE;并且与 Quiz3 按钮等数据库中的 Quiz3 TABLE 相同。

到目前为止,我的 android 应用程序仅适用于 Quiz1,因为它只从我的数据库中检索一个用于 Quiz1 的表。

【问题讨论】:

  • 为什么每个测验都使用单独的表格?您应该有一个带有测验编号列的表格。
  • 注明。我将在我的桌子上添加问题编号,而不是使用不同的桌子。但如果是这种情况,我应该如何处理我的 SELECT QUERY 语句?

标签: java android sqlite mobile


【解决方案1】:

您必须将表名更改为Quiz2等。 这要求函数需要知道测验编号:

public List<Question> getQuestionSet(int quizNr, int numQ) {
    ...
    rawQuery("SELECT * FROM Quiz" + quizNr + " ORDER BY random() LIMIT " + numQ, null);
    ...
}

如果您只有一个表,则必须过滤相应的列:

public List<Question> getQuestionSet(int quizNr, int numQ) {
    ...
    rawQuery("SELECT * FROM Quiz WHERE QuizNr = " + quizNr + " ORDER BY random() LIMIT " + numQ, null);
    ...
}

【讨论】:

  • 已经使用了带有问题编号列的单个表格。我现在应该在 Quiz.java - getQuizNumber 方法上放什么来检索测验的适当问题?想到一个循环语句,但我不知道该怎么做
  • 您显示的代码中没有 getQuizNumber 函数。要提出新问题,请使用“”按钮。
  • 感谢这对我有用。 @CL。请您认为您可以就这个问题给我您的意见stackoverflow.com/questions/25598696/…
猜你喜欢
  • 2019-08-24
  • 2020-08-01
  • 2015-11-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多