我有一个棘手的问题。我的应用程序将用户数据输入到 SQLite 数据库中,并且我已经设法编写查询以取回数据。
这包括获取不同值列表的查询,示例如下。假设您有以下数据:
Column a | Column b | Column c
a a a
a b b
a b b
b b b
使用“分组依据”功能,查询将创建以下列表:
aaa
abb
bbb
所以我正在尝试,但未能做的是扩展它并计算每个不同条目出现的次数 - 所以在上述情况下,它看起来像这样:
1 x aaa
2 x abb
1 x bbb
那么有没有一种方法,一旦你查询了数据库,就可以从光标中获取每个不同条目的出现次数 - 或者我是否需要编写一个单独的查询来获取这些信息..
这是我目前的查询:
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;
}
有关其他信息,这是我调用查询并检索数据的主要活动中的代码:
final Cursor componentsAndSpecCursor = (Cursor) rmDbHelper.fetchComponentsAndSpecForManufacturer(inspectionId, rackingSystem, manufacturer);
if(componentsAndSpecCursor.moveToFirst()){
do {
componentType = componentsAndSpecCursor.getString(componentsAndSpecCursor.getColumnIndex(RMDbAdapter.COMPONENT_TYPE));
specText1 = componentsAndSpecCursor.getString(componentsAndSpecCursor.getColumnIndex(RMDbAdapter.TEXT1));
specText2 = componentsAndSpecCursor.getString(componentsAndSpecCursor.getColumnIndex(RMDbAdapter.TEXT2));
specText3 = componentsAndSpecCursor.getString(componentsAndSpecCursor.getColumnIndex(RMDbAdapter.TEXT3));
specText4 = componentsAndSpecCursor.getString(componentsAndSpecCursor.getColumnIndex(RMDbAdapter.TEXT4));
specNotes = componentsAndSpecCursor.getString(componentsAndSpecCursor.getColumnIndex(RMDbAdapter.NOTES_SPEC));
message.append(componentType).append(" ").append(specText1).append(" ").append(specText2).append(" ").append(specText3).append(" ").append(specText4).append(" ").append(specNotes).append("\r\n");
}
while (componentsAndSpecCursor.moveToNext());
}
componentsAndSpecCursor.close();
所以我也不知道一旦我在查询中包含了这段代码(我仍然看不到它是如何完成的!),我将如何获得计数。