Android应用程序组件Content Provider应用实例( 四 )

< 0) {throw new SQLiteException("Unable to insert " + values + " for " + uri);}Uri newUri = ContentUris.withAppendedId(uri, id);resolver.notifyChange(newUri, null);return newUri;}@Overridepublic int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {SQLiteDatabase db = dbHelper.getWritableDatabase();int count = 0;switch(uriMatcher.match(uri)) {case Articles.ITEM: {count = db.update(DB_TABLE, values, selection, selectionArgs);break;}case Articles.ITEM_ID: {String id = uri.getPathSegments().get(1);count = db.update(DB_TABLE, values, Articles.ID + "=" + id+ (!TextUtils.isEmpty(selection) ? " and (" + selection + ')' : ""), selectionArgs);break;}default:throw new IllegalArgumentException("Error Uri: " + uri);}resolver.notifyChange(uri, null);return count;}@Overridepublic int delete(Uri uri, String selection, String[] selectionArgs) {SQLiteDatabase db = dbHelper.getWritableDatabase();int count = 0;switch(uriMatcher.match(uri)) {case Articles.ITEM: {count = db.delete(DB_TABLE, selection, selectionArgs);break;}case Articles.ITEM_ID: {String id = uri.getPathSegments().get(1);count = db.delete(DB_TABLE, Articles.ID + "=" + id+ (!TextUtils.isEmpty(selection) ? " and (" + selection + ')' : ""), selectionArgs);break;}default:throw new IllegalArgumentException("Error Uri: " + uri);}resolver.notifyChange(uri, null);return count;}@Overridepublic Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {Log.i(LOG_TAG, "ArticlesProvider.query: " + uri);SQLiteDatabase db = dbHelper.getReadableDatabase();SQLiteQueryBuilder sqlBuilder = new SQLiteQueryBuilder();String limit = null;switch (uriMatcher.match(uri)) {case Articles.ITEM: {sqlBuilder.setTables(DB_TABLE);sqlBuilder.setProjectionMap(articleProjectionMap);break;}case Articles.ITEM_ID: {String id = uri.getPathSegments().get(1);sqlBuilder.setTables(DB_TABLE);sqlBuilder.setProjectionMap(articleProjectionMap);sqlBuilder.appendWhere(Articles.ID + "=" + id);break;}case Articles.ITEM_POS: {String pos = uri.getPathSegments().get(1);sqlBuilder.setTables(DB_TABLE);sqlBuilder.setProjectionMap(articleProjectionMap);limit = pos + ", 1";break;}default:throw new IllegalArgumentException("Error Uri: " + uri);}Cursor cursor = sqlBuilder.query(db, projection, selection, selectionArgs, null, null, TextUtils.isEmpty(sortOrder) ? Articles.DEFAULT_SORT_ORDER : sortOrder, limit);cursor.setNotificationUri(resolver, uri);return cursor;}@Overridepublic Bundle call(String method, String request, Bundle args) {Log.i(LOG_TAG, "ArticlesProvider.call: " + method);if(method.equals(Articles.METHOD_GET_ITEM_COUNT)) {return getItemCount();}throw new IllegalArgumentException("Error method call: " + method);}private Bundle getItemCount() {Log.i(LOG_TAG, "ArticlesProvider.getItemCount");SQLiteDatabase db = dbHelper.getReadableDatabase();Cursor cursor = db.rawQuery("select count(*) from " + DB_TABLE, null);int count = 0;if (cursor.moveToFirst()) {count = cursor.getInt(0);}Bundle bundle = new Bundle();bundle.putInt(Articles.KEY_ITEM_COUNT, count);cursor.close();db.close();return bundle;}private static class DBHelper extends SQLiteOpenHelper {public DBHelper(Context context, String name, CursorFactory factory, int version) {super(context, name, factory, version);}@Overridepublic void onCreate(SQLiteDatabase db) {db.execSQL(DB_CREATE);}@Overridepublic void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {db.execSQL("DROP TABLE IF EXISTS " + DB_TABLE);onCreate(db);}}}
我们在实现自己的 时,必须继承于类,并且实现以下六个函数:
-- (),用来执行一些初始化的工作 。
-- query(Uri, [], , [], ),用来返回数据给调用者 。
-- (Uri, ),用来插入新的数据 。
-- (Uri, , , []),用来更新已有的数据 。
-- (Uri, , []),用来删除数据 。
-- (Uri),用来返回数据的MIME类型 。
这些函数的实现都比较简单,这里我们就不详细介绍了,主要解释五个要点 。
第一点是我们在类的内部中定义了一个类,它继承于类,它用是用辅助我们操作数据库的 。使用这个类来辅助操作数据库的好处是只有当我们第一次对数据库时行操作时,系统才会执行打开数据库文件的操作 。拿我们这个例子来说,只有第三方应用程序第一次调用query、、或者函数来操作数据库时,我们才会真正去打开相应的数据库文件 。这样在函数里,就不用执行打开数据库的操作,因为这是一个耗时的操作,而在函数中,要避免执行这些耗时的操作 。