收集NOCASE错误?

时间:2014-12-29 03:05:31

标签: android sqlite

目前在Android 5.x上我的应用程序遇到SQLite查询问题。 ORDER BY " + KEY_TEMP_CAT_CATEGORY + " COLLATE NOCASE似乎存在问题。这导致我的查询从结果中排除一些项目。删除此特定部分会导致问题消失。在Android 5.x之前,这不是一个问题:(

我将非常感谢您如何解决此问题的任何提示或想法。

以下是完整查询:

// query for total categories
String queryTotalCat = 
    "SELECT _id, NULL as " + KEY_TEMP_CAT_CATEGORY + ", " +
    "COUNT(*) as " + KEY_TEMP_CAT_COUNT + " " +
    "FROM " + TABLE_NAME + " " +
    "WHERE " + KEY_TYPE_FAVORITE + "!=0";

// query for total count of each repeated categories
String queryTotalRows =
    "SELECT _id, " + KEY_CATEGORY + " as " + KEY_TEMP_CAT_CATEGORY + ", " +
    "COUNT(" + KEY_CATEGORY + ") as " + KEY_TEMP_CAT_COUNT + " " +
    "FROM " + TABLE_NAME + " " +
    "WHERE " + KEY_TYPE_FAVORITE + "!=0";

// union the two queries
String query = 
    queryTotalCat + " UNION " + queryTotalRows + " " +
    "ORDER BY "+ KEY_TEMP_CAT_CATEGORY + " " +
    "COLLATE NOCASE ASC";

1 个答案:

答案 0 :(得分:0)

我尝试简化查询以查看确切的事情在哪里工作,我能够找到一些东西。

<强> COUNT
COUNT调用实际上缺少GROUP BY调用以指定我需要分组的列。

<强> UNION
我将两个SELECT调用放在UNION上的顺序也是解决问题的关键。需要首先放置具有GROUP BY的复杂复合SELECT调用,然后再进行简单的SELECT调用。

修改后的工作查询

// query for total categories
String queryTotalCat = 
    "SELECT " +
        "0 AS _id," +
        "NULL AS " + KEY_TEMP_CAT_CATEGORY + "," +
        "COUNT(*) AS " + KEY_TEMP_CAT_COUNT +
    " FROM " + TABLE_NAME + 
    " WHERE " + KEY_TYPE_FAVORITE + "=1";

// query for total count of each repeated categories
String queryTotalRows =
    "SELECT " + 
        "_id, " + 
        KEY_CATEGORY + " AS " + KEY_TEMP_CAT_CATEGORY + "," +
        "COUNT(" + KEY_CATEGORY + ") AS " + KEY_TEMP_CAT_COUNT + 
    " FROM " + TABLE_NAME +
    " WHERE " + KEY_TYPE_FAVORITE + "=1" +
    " GROUP BY " + KEY_CATEGORY;

// union the two queries
String query = 
        queryTotalRows + " UNION " + queryTotalCat +
    " ORDER BY "+ KEY_TEMP_CAT_CATEGORY + " COLLATE NOCASE ASC";