在SQLite中通过2个参数过滤字符串选择 [英] String selection filtering by 2 parameters in SQLite
问题描述
无法弄清楚如何在连接的表中正确过滤.当我使用时:
Can not figure out how correctly filter inside my joined tables. When I use:
字符串选择="quiz_list_id" +"=?" +"AND" +"LevelID" +"=?";
当我点击从数据库正确分配了名称的按钮时,什么也没有发生.我应该在哪里寻找缺少的参数?
Nothing happens when i hit button which has correctly assigned name from database. Where should I look to add missing parameters?
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,它将信息传递给主要活动
Here my StartingScreenActivity that pass information to main activity
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);
}
推荐答案
每个人都感谢您的建议.最终问题是我没有将查询中的参数之一传递给startQuiz方法.相反,我传递的是所选按钮的ID.
Everyone thank you for the suggestions. Eventually the problem was that I did not pass one of the parameter from my query to the startQuiz method. Instead I was passing id of the selected button.
更改:
来自
startQuiz(v.getId(), categoryName.get(index).toString());
到
startQuiz(TestLevel, categoryName.get(index).getId(), categoryName.get(index).toString());
,并在下面的方法中添加了另一个必需的参数:
and in the method below added another one required parameter:
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);
}
这篇关于在SQLite中通过2个参数过滤字符串选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!