我有两张桌子(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 我需要计算exam_id=2中有多少正确的答案=‘y’。
我尝试下面的代码,但它返回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“。
给我一些提示或推荐信。
任何帮助都是非常感谢的。
提前感谢
发布于 2012-04-08 13:23:55
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 +"'");上面是你的查询,我已经格式化了。
希望这对你有帮助。
https://stackoverflow.com/questions/10060567
复制相似问题