@Override public Cursor query( Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { SQLiteQueryBuilder qb = new SQLiteQueryBuilder(); switch (uriMatcher.match(uri)) { case TEMP_MATTERS: qb.setTables(TempMatterTable.TABLE_NAME); qb.setProjectionMap(tempMatterProjectionMap); break; case TEMP_MATTER_ID: qb.setTables(TempMatterTable.TABLE_NAME); qb.setProjectionMap(tempMatterProjectionMap); qb.appendWhere(TempMatterTable._ID + "=" + uri.getPathSegments().get(1)); break; default: throw new IllegalArgumentException("Unknown URI " + uri); } String orderBy; if (TextUtils.isEmpty(sortOrder)) { orderBy = TempMatterTable.DEFAULT_SORT_ORDER; } else { orderBy = sortOrder; } SQLiteDatabase db = dbHelper.getReadableDatabase(); Cursor c = qb.query(db, projection, selection, selectionArgs, null, null, orderBy); // 为Cursor对象注册一个观察数据变化的URI c.setNotificationUri(getContext().getContentResolver(), uri); return c; }
@Override public Cursor query( Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { SQLiteDatabase db = dbHelper.getReadableDatabase(); Cursor cursor = null; switch (uriMatcher.match(uri)) { case BOOK_DIR: cursor = db.query("Book", projection, selection, selectionArgs, null, null, sortOrder); break; case BOOK_ITEM: String bookId = uri.getPathSegments().get(1); cursor = db.query("Book", projection, "id = ?", new String[] {bookId}, null, null, sortOrder); break; case CATEGORY_DIR: cursor = db.query("Category", projection, selection, selectionArgs, null, null, sortOrder); break; case CATEGORY_ITEM: String categoryId = uri.getPathSegments().get(1); cursor = db.query( "Category", projection, "id = ?", new String[] {categoryId}, null, null, sortOrder); break; default: break; } return cursor; }
public SampleDBManager openReadable() { db = DBHelper.getReadableDatabase(); return this; } // end openReadable