首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Android-SQLite:如何统计列中的特定值?

Android-SQLite:如何统计列中的特定值?
EN

Stack Overflow用户
提问于 2012-04-08 13:15:39
回答 1查看 2.8K关注 0票数 0

我有两张桌子(TABLE_EXAM,TABLE_RESULT)。这是我的TABLE_RESULT的价值。

代码语言:javascript
复制
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。

代码语言:javascript
复制
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“。

给我一些提示或推荐信。

任何帮助都是非常感谢的。

提前感谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-04-08 13:23:55

代码语言:javascript
复制
select count(*) from TABLE_RESULT where correct_answer="y" and exam id=2;

这是值为y和检查id=2的总行数

只需尝试运行此查询,然后将参数添加到其中。

代码语言:javascript
复制
SELECT COUNT(*)FROM result WHERE exam_id =" + examId + " and correctAnswer ='" + confirmAnswer +"'");

上面是你的查询,我已经格式化了。

希望这对你有帮助。

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10060567

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档