无法弄清楚如何在连接的表中正确过滤。 当我使用时:
String selection = " quiz_list_id " + " = ? " + " AND " + " LevelID " + " = ? ";
当我点击从数据库正确分配了名称的按钮时,什么也没有发生。 我应该在哪里寻找缺少的参数?
public ArrayList<Question> getNLevelQuestions( int TestLevelID, int quizListID) {
ArrayList<Question> questionList = new ArrayList<>();
db = getReadableDatabase();
String table = QuestionsTable.TABLE_NAME + " JOIN " + QuizListTable.TABLE_NAME +
" ON " + QuizListTable._ID + " = " + QuestionsTable.COLUMN_QUIZ_LIST_ID;
String selection = "quiz_list_id" + " = ? " + " AND " + "LevelID" + " = ?";
String[] selectionArgs = new String[]{String.valueOf(quizListID), String.valueOf(TestLevelID)};
Cursor c = db.query(table,
null,
selection,
selectionArgs,
null,
null,
null
);
if (c.moveToFirst()) {
do {
Question question = new Question();
question.setId(c.getInt(c.getColumnIndex("question_id")));
question.setQuestion(c.getString(c.getColumnIndex("question")));
question.setOption1(c.getString(c.getColumnIndex("option1")));
question.setOption2(c.getString(c.getColumnIndex("option2")));
question.setOption3(c.getString(c.getColumnIndex("option3")));
question.setOption4(c.getString(c.getColumnIndex("option4")));
question.setAnswerNB(c.getInt(c.getColumnIndex("answer_nb")));
question.setQuizListID(c.getInt(c.getColumnIndex("quiz_list_id")));
questionList.add(question);
} while (c.moveToNext());
}
c.close();
return questionList;
}
这是我的StartingScreenActivity,它将信息传递给主要活动
PracticeList = findViewById(R.id.Practice_n_group);
for(int i=0; i<categorySize;i++){
practiceButton = new Button(this);
practiceButton.setText("" + categoryName.get(i));
practiceButton.setId(i+1);
final int index = i;
practiceButton.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
practiceButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startQuiz(v.getId(), categoryName.get(index).toString());
}
});
// adding button to layout
PracticeList.addView(practiceButton);
}
}
private void startQuiz(int practiceListID, String quizListName) {
Intent intent = new Intent(StartingScreenActivity.this, MainActivity.class);
intent.putExtra(EXTRA_PRACTICE_ID, practiceListID);
intent.putExtra(EXTRA_PRACTICE_NAME, quizListName);
startActivityForResult(intent, REQUEST_CODE_QUIZ);
}
答案 0 :(得分:0)
请尝试以下操作(更新查询以及选择args字符串):
String table = "SELECT * FROM " + QuizListTable.TABLE_NAME + " a INNER JOIN " + QuestionsTable.TABLE_NAME + " b ON " + QuizListTable._ID + " = " + QuestionsTable.COLUMN_QUIZ_LIST_ID;
String[] columns = new String[]{
"question_id",
"question",
"option1",
"option2",
"option3",
"option4",
"quiz_list_id",
"answer_nb",
"_id",
"name",
"LevelID"
};
String selection = " b.LevelID=? " + " AND " + " a.quiz_list_id=?";
String[] selectionArgs = new String[]{ String.valueOf(JLPTID), String.valueOf(quizListID)};
Cursor c = db.query(
table,
columns,
selection,
selectionArgs,
null,
null,
null
);
答案 1 :(得分:0)
您不能将方法query()用于sql语句,而只能查询表。
您必须使用rawQuery()方法。
因此,将完整的sql语句设置为:
String sql ="SELECT .... WHERE LevelID = ? and quiz_list_id = ?";
然后:
String[] selectionArgs = new String[]{ String.valueOf(JLPTID), String.valueOf(quizListID)};
Cursor c = db.rawQuery(sql, selectionArgs);
注意:在您的sql语句中,对列使用适当的限定符,因此没有歧义的列名。
答案 2 :(得分:0)
每个人都感谢您的建议。最终问题是我没有将查询中的参数之一传递给startQuiz方法。相反,我传递的是所选按钮的ID。
更改:
来自
startQuiz(v.getId(), categoryName.get(index).toString());
到
startQuiz(TestLevel, categoryName.get(index).getId(), categoryName.get(index).toString());
,并在下面的方法中添加了另一个必需参数:
private void startQuiz(int testLevel, int practiceListID, String quizListName) {
Intent intent = new Intent(StartingScreenActivity.this, MainActivity.class);
intent.putExtra(EXTRA_LEVEL_ID, testLevel);
intent.putExtra(EXTRA_PRACTICE_ID, practiceListID);
intent.putExtra(EXTRA_PRACTICE_NAME, quizListName);
startActivityForResult(intent, REQUEST_CODE_QUIZ);
}