2

我了解如何创建查询以仅查找不同的值,但我想计算每个不同的值有多少。

所以我的代码示例,在我的数据库助手中,它只是给了我一个不同值的列表:

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)),"");

但是我如何获取计数值呢?

提前致谢。

4

1 回答 1

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));
于 2012-12-21T16:42:47.763 回答