我有两个表(TABLE_EXAM,TABLE_RESULT)。这是我的TABLE_RESULT的值。
result_id exam_id question_id correct_answer
1 2 4 y
2 2 5 y
3 2 6 n
4 2 7 y
我需要计算correct_answer ='y'的数量,其中exam_id = 2 我尝试下面的代码,但它返回0.
public int calculateResult(int examId,String confirmAnswer)
{
int correctAnswer=0;
try
{
SQLiteDatabase db=this.getWritableDatabase();
String selectQuery=("select count(correctAnswer) from result where exam_id ='" + examId + "' and correctAnswer ='" + 'y' +"'" );
// String selectQuery=("SELECT COUNT(*)FROM result WHERE exam_id ='" + examId + "' and correctAnswer ='" + confirmAnswer +"'" );
Cursor cursor = db.rawQuery(selectQuery, null);
if(cursor.moveToLast())
{
correctAnswer=cursor.getInt(3);
}
}
catch(Exception e)
{
e.printStackTrace();
}
return correctAnswer;
}
在变量confirm_answer中,我传递“y”
给我一些提示或参考。
任何帮助表示赞赏
在此先感谢
答案 0 :(得分:4)
select count(*) from TABLE_RESULT where correct_answer="y" and exam id=2;
这是值为y且检验ID = 2
的总行数只需尝试运行此查询,然后只需添加参数即可。
SELECT COUNT(*)FROM result WHERE exam_id =" + examId + " and correctAnswer ='" + confirmAnswer +"'");
以上是我格式化的查询。
希望这会对你有所帮助。