我嘗試在自定義contentprovider上運行此查詢。Android - 在contentProvider中選擇max
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
我的內容提供者實現(標準 - 見記事本樣品)
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;
}
你能幫助我嗎?
問候
在MyTouch操作系統2.1(不知道SQLite的版本)上,我的語法對我有效,但在我的G2上失敗:( – mobibob 2011-04-22 06:09:16