得到的时间发生不同的条目中的数据库查询的次数的计数(Android的) [英] Get a count of the number of times distinct entries occur in database query (Android)

查看:106
本文介绍了得到的时间发生不同的条目中的数据库查询的次数的计数(Android的)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个有点棘手的问题。我的应用程序输入用户数据到SQLite数据库和我已成功地编写查询来获取数据的退了出去。

I have a bit of a tricky problem. My app inputs user data into the SQLite database and I have managed to write queries to get the data back out.

这包括查询,获取不同值的列表中,例如可以如下。说你有以下数据:

This includes a query that gets a list of distinct values, an example may be as follows. Say you have the following data:

Column a  |  Column b  | Column c
   a            a           a
   a            b           b
   a            b           b
   b            b           b

按组功能使用,查询将创建以下列表:

Using the 'group by' function, the query would create the following list:

aaa
abb
bbb

那么,我想,但没有做的是扩大这一点,让每个不同的项,则会出现次数的计数 - 因此,在上述情况下,它应该是这样的:

So what I am trying, but failing to do is expand this and get a count of the number of times each distinct entry occurs - so in the above case it would look like this:

1 x aaa
2 x abb
1 x bbb

那么,有没有一种方式,一旦你查询的数据库,让每个不同项的occurancies的计数,从光标 - 或者我需要写一个单独的查询来获得这些信息。

So is there a way, once you have queried the database, to get a count of the occurancies of each distinct entry, from the Cursor - or do I need to write a seperate query to get this information..

下面是我的查询,因为它看起来的时刻:

Here is my query as it looks at the moment:

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;
}

有关更多的信息,这里是我的主要活动在这里我所说的查询和检索数据code:

For additional information, here is the code in my main activity where I call the query and retrieve the data:

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();

所以我也是不知道我将如何得到伯爵一旦我已包括在我的查询该位code的(我仍然无法看到它是怎么做的!)。

So I am also unaware of how I would get the Count once I have included this bit of code in my query (which I still can't see how it is done!).

推荐答案

这是你想要的查询是:

select count(*) as cnt, cola, colb, colc
from YourTable
group by cola, colb, colc

我不知道如何把这个到code。你似乎不有,在你提供的code的代码片段返回组合查询。

I'm not sure how to put this into your code. You don't seem to have a query that returns the combinations in the snippet of code that you provided.

这篇关于得到的时间发生不同的条目中的数据库查询的次数的计数(Android的)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆