簡體   English   中英

Android - 在contentProvider中選擇最大值

[英]Android - Select max in contentProvider

我嘗試在我的自定義內容提供者上運行此查詢。

cursor = activity.getContentResolver().query(
                GoalDescriptor.CONTENT_URI,
                "max(priority)", null,
                null, null);

獲取優先級int列的最大值。

我也嘗試過:

cursor = activity.getContentResolver().query(
                GoalDescriptor.CONTENT_URI,
                null, "max(priority)",
                null, null);

沒有成功。

此代碼返回此異常:

    java.lang.IllegalArgumentException: Invalid column MAX(priority)
E/DatabaseUtils(  688):     at android.database.sqlite.SQLiteQueryBuilder.computeProjection(SQLiteQueryBuilder.java:523)
E/DatabaseUtils(  688):     at android.database.sqlite.SQLiteQueryBuilder.buildQuery(SQLiteQueryBuilder.java:370)
E/DatabaseUtils(  688):     at android.database.sqlite.SQLiteQueryBuilder.query(SQLiteQueryBuilder.java:323)
E/DatabaseUtils(  688):     at android.database.sqlite.SQLiteQueryBuilder.query(SQLiteQueryBuilder.java:280)
E/DatabaseUtils(  688):     at nan.salsa.contentprovider.goal.GoalContentProvider.query(GoalContentProvider.java:118)
E/DatabaseUtils(  688):     at android.content.ContentProvider$Transport.bulkQuery(ContentProvider.java:150)
E/DatabaseUtils(  688):     at android.content.ContentProviderNative.onTransact(ContentProviderNative.java:111)
E/DatabaseUtils(  688):     at android.os.Binder.execTransact(Binder.java:288)
E/DatabaseUtils(  688):     at dalvik.system.NativeStart.run(Native Method)
D/AndroidRuntime(  727): Shutting down VM

我的內容提供商實施(標准 - 請參閱notePad示例)

public Cursor query(Uri uri, String[] projection, String selection,
        String[] selectionArgs, String sortOrder) {
    SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
    qb.setTables(GoalDescriptor.TABLE_NAME);

    switch (sUriMatcher.match(uri)) {

    case GOAL:
        qb.setProjectionMap(sGoalProjectionMap);
        qb.appendWhere(GoalDescriptor._ID + "="
                + uri.getPathSegments().get(1));
        break;

    case GOALS:
        qb.setProjectionMap(sGoalProjectionMap);
        break;

    default:
        throw new IllegalArgumentException("Unknown URI " + uri);
    }

    // If no sort order is specified use the default
    String orderBy;
    if (TextUtils.isEmpty(sortOrder)) {
        orderBy = GoalInfo.GoalDescriptor.DEFAULT_SORT_ORDER;
    } else {
        orderBy = sortOrder;
    }

    // Get the database and run the query
    SQLiteDatabase db = mOpenHelper.getReadableDatabase();
    Cursor c = qb.query(db, projection, selection, selectionArgs, null,
            null, orderBy);

    // Tell the cursor what uri to watch, so it knows when its source data
    // changes
    c.setNotificationUri(getContext().getContentResolver(), uri);
    return c;
}

你能幫助我嗎 ?

問候

您正在發送最大(優先級)作為投影,將其作為選擇發送,它應該工作。

看到這個問題: Android:獲取列中的最高價值

編輯:

它似乎應該工作:

cursor = activity.getContentResolver().query(
         GoalDescriptor.CONTENT_URI,
          new String[] {"MAX(priority) AS max_priority"}, null,
         null, null);

不幸的是,nEx的答案(使用“AS”為列提供別名)僅適用於未在其SQLiteQueryBuilder中設置strictProjectionMap標志的提供程序。 例如,聯系人提供程序設置此標志,因此如果您嘗試AS方法,您仍將收到“無效列”錯誤。

如果有人能告訴我如何在選擇中指定一個像MAX這樣的聚合函數,我會非常感激。 如果我嘗試它,我只是得到“濫用聚合函數”異常,這正是我期望嘗試將聚合函數放在WHERE子句中。 我知道你可以在WHERE子句中放置一個子查詢,但這假定你知道實際的表結構,並且它永遠不會在任何給定的設備或操作系統級別上更改,我不想打賭(畢竟,這就是為什么首先使用提供者接口,從底層數據存儲中抽象出來的原因。

所以...我不知道為設置strictProjectionMap標志的提供者做任何方法,除了用所需字段對DESC進行排序,如果提供者支持它,最好LIMIT為1行(或者如果你願意的話)通過在排序字符串中包含LIMIT來抓住機會。 如果有人知道這些案件的其他方法,我會很高興!

在我的情況下,nEx的答案實際上有效,但我忘了在光標的getColumnIndexByName()方法中添加“max _”+ COLUMN_NAME。

在我發現這個也有待改變之后,它終於奏效了!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM