如何计算每个不同的值(SQLite查询 - Android)

时间:2012-12-20 20:41:19

标签: android sqlite

我理解如何创建查询以仅查找不同的值,但我想计算每个不同值的数量。

这是我的代码示例,在我的数据库帮助器中,它只给出了一个不同值的列表:

public Cursor fetchDamagedComponentSpecForInspection(long inspectionId, String componentType) {
    Cursor mCursor =
            rmDb.query(true, DAMAGED_COMPONENTS_TABLE, new String[] {
                    DAMAGED_COMPONENT_ID,
                    LOCATION_LINK,
                    RUN_LINK,
                    AREA_LINK,
                    INSPECTION_LINK,
                    LOCATION_REF,
                    RACKING_SYSTEM,
                    COMPONENT,
                    COMPONENT_TYPE,
                    QUANTITY,
                    POSITION,
                    RISK,
                    ACTION_REQUIRED,
                    NOTES_GENERAL,
                    MANUFACTURER,
                    TEXT1,
                    TEXT2,
                    TEXT3,
                    TEXT4,
                    NOTES_SPEC,
                    SPEC_SAVED}, 
                    INSPECTION_LINK + " = " + inspectionId + " AND " + COMPONENT_TYPE + " = ? AND " + SPEC_SAVED + " = ? ", 
                    new String[] {componentType, "Yes"},
                    MANUFACTURER + ", " + TEXT1 + ", " + TEXT2 + ", " + TEXT3 + ", " + TEXT4 + ", " + NOTES_SPEC, 
                    null, null, null);
        if (mCursor != null) {
            mCursor.moveToFirst();
        }
        return mCursor;
}

所以这会返回一个类似的列表:

Dexion, M Duty, 3000mm, Blue
Hi-lo, Rackplan, 4000mm, Blue
PSS, P85 1.6, 4500mm, Blue

但是我想知道每个值有多少(即有多少'Dexion,M Duty,3000mm,Blue'条目,所以输出看起来有点像这样:

2 x Dexion, M Duty, 3000mm, Blue
5 x Hi-lo, Rackplan, 4000mm, Blue
1 x PSS, P85 1.6, 4500mm, Blue

CommonsWare建议发布一个帖子,提供以下解决方案:

select type, count(type) from table group by type;

但是,我不确定如何使用我正在使用的查询类型将其集成到现有代码中。

编辑 - 下面是我尝试整合此计数功能但没有成功:

public Cursor fetchDamagedComponentSpecForInspection(long inspectionId, String componentType) {
    Cursor mCursor =
            rmDb.query(true, DAMAGED_COMPONENTS_TABLE, new String[] {
                    DAMAGED_COMPONENT_ID,
                    LOCATION_LINK,
                    RUN_LINK,
                    AREA_LINK,
                    INSPECTION_LINK,
                    LOCATION_REF,
                    RACKING_SYSTEM,
                    COMPONENT,
                    COMPONENT_TYPE,
                    QUANTITY,
                    POSITION,
                    RISK,
                    ACTION_REQUIRED,
                    NOTES_GENERAL,
                    MANUFACTURER,
                    TEXT1,
                    TEXT2,
                    TEXT3,
                    TEXT4,
                    NOTES_SPEC,
                    SPEC_SAVED}, 
                    INSPECTION_LINK + " = " + inspectionId + " AND " + COMPONENT_TYPE + " = ? AND " + SPEC_SAVED + " = ? ", 
                    new String[] {componentType, "Yes"},
                    MANUFACTURER + ", " + TEXT1 + ", " + TEXT2 + ", " + TEXT3 + ", " + TEXT4 + ", " + NOTES_SPEC + 
                    ", Count (*) AS count", 
                    null, null, null);
        if (mCursor != null) {
            mCursor.moveToFirst();
        }
        return mCursor;
}

也无法理解我是如何在我的主代码中实际检索到这一点的(下面是我现在如何获取位数):

        manufacturer = RMUtilities.notEmpty(cursor.getString(cursor.getColumnIndex(RMDbAdapter.MANUFACTURER)),"");
        text_1 = RMUtilities.notEmpty(cursor.getString(cursor.getColumnIndex(RMDbAdapter.TEXT1)),"");
        text_2 = RMUtilities.notEmpty(cursor.getString(cursor.getColumnIndex(RMDbAdapter.TEXT2)),"");
        text_3 = RMUtilities.notEmpty(cursor.getString(cursor.getColumnIndex(RMDbAdapter.TEXT3)),"");
        text_4 = RMUtilities.notEmpty(cursor.getString(cursor.getColumnIndex(RMDbAdapter.TEXT4)),"");
        spec_notes = RMUtilities.notEmpty(cursor.getString(cursor.getColumnIndex(RMDbAdapter.NOTES_SPEC)),"");

但我如何获取计数值?

提前致谢。

1 个答案:

答案 0 :(得分:0)

从我的代码:

String w = myColumn + "like ?"
String [] args = { myData }
c = ourDatabase.query(DATABASE_TABLE, columns, w, args, null, null, null);
int count = c.getCount(c.getColumnIndex(myColumn));