0
私は2つのテーブル(TABLE_EXAM、TABLE_RESULT)を持っています。ここに私のTABLE_RESULTの値があります。Android-SQLite:列から特定の値を数えるには?
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の場合、correct_answer = 'y'の数を計算する必要があります。
私は、次のコードを試してみたが、それは私が「Y」を渡す変数confirm_answerは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;
}
を返します。
ヒントやリファレンスを教えてください。
ご協力いただければ幸いです。アドバンス
@sanpatil:あなたの間違いは、私が今削除したクエリの文字列として2を与えていたと思います。 – Bhavin
ありがとうございます...いいえ、実際には値2を含むexam_idを渡します....最初のクエリはSQLiteブラウザで実行されますが、プログラムでチェックする必要があります..... –