Android資料庫ContentProvider封裝原理

l_serein發表於2012-07-12

大家都知道Android資料庫使用的是SQLite,但是Google為了給我們簡化操作,可以不用經常編寫容易出錯的SQL語句,直接通過ContentProvider來封裝資料的query查詢、新增insert、刪除delete和更新update,我們就以Android系統的SDK中的例子來給大家簡單的說明下吧。

public class NotePadProvider extends ContentProvider {

private static final String TAG = "NotePadProvider";

private static final String DATABASE_NAME = "note_pad.db"; //資料庫儲存檔名,包含了.db字尾

private static final int DATABASE_VERSION = 2; //資料庫版本號,這個是自己定義的,未來擴充套件資料庫時自己可以方便的定義升級規則

private static final String NOTES_TABLE_NAME = "notes"; //表名

private static HashMap sNotesProjectionMap; //常規的Notes

private static HashMap sLiveFolderProjectionMap; //LiveFoder內容

private static final int NOTES = 1;

private static final int NOTE_ID = 2;

private static final int LIVE_FOLDER_NOTES = 3;

private static final UriMatcher sUriMatcher; //這裡Android123提示大家,通常我們運算元據庫的Uri比如content://android123/cwj/1103這樣的Uri均通過UriMatcher註冊並識別的。

private static class DatabaseHelper extends SQLiteOpenHelper { //資料庫輔助子類

DatabaseHelper(Context context) {

super(context, DATABASE_NAME, null, DATABASE_VERSION);

}

@Override

public void onCreate(SQLiteDatabase db) { //首次生成資料庫,執行sql命令建立一個表

db.execSQL("CREATE TABLE " + NOTES_TABLE_NAME + " ("

+ Notes._ID + " INTEGER PRIMARY KEY,"

+ Notes.TITLE + " TEXT,"

+ Notes.NOTE + " TEXT,"

+ Notes.CREATED_DATE + " INTEGER,"

+ Notes.MODIFIED_DATE + " INTEGER"

+ ");");

}

@Override

public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { //剛來資料的版本,就是為了定義我們如果未來資料庫需要擴充套件,幫助使用者識別並根據規則自動升級資料庫檔案

Log.w(TAG, "Upgrading database from version " + oldVersion + " to "

+ newVersion + ", which will destroy all old data");

db.execSQL("DROP TABLE IF EXISTS notes"); //由於這裡沒有做細節處理,如果有新版本,刪除老的表,我們未來不能這樣處理,這僅僅是Google的例子而已所以刪除老版本資料

onCreate(db);

}

}

private DatabaseHelper mOpenHelper;

@Override

public boolean onCreate() { //這裡重寫ContentProvider的onCreate方法做一些初始化操作

mOpenHelper = new DatabaseHelper(getContext());

return true;

}

//有關資料庫的查詢操作,Android的SQLite提供了一個SQLiteQueryBuilder方法再次將SQL命令封裝了下,單獨分離出表名,排序方法等

@Override

public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs,

String sortOrder) {

SQLiteQueryBuilder qb = new SQLiteQueryBuilder();

qb.setTables(NOTES_TABLE_NAME);

switch (sUriMatcher.match(uri)) {

case NOTES:

qb.setProjectionMap(sNotesProjectionMap);

break;

case NOTE_ID:

qb.setProjectionMap(sNotesProjectionMap);

qb.appendWhere(Notes._ID + "=" + uri.getPathSegments().get(1));

break;

case LIVE_FOLDER_NOTES:

qb.setProjectionMap(sLiveFolderProjectionMap);

break;

default:

throw new IllegalArgumentException("Unknown URI " + uri);

}

String orderBy;

if (TextUtils.isEmpty(sortOrder)) {

orderBy = NotePad.Notes.DEFAULT_SORT_ORDER;

} else {

orderBy = sortOrder;

}

SQLiteDatabase db = mOpenHelper.getReadableDatabase();

Cursor c = qb.query(db, projection, selection, selectionArgs, null, null, orderBy);

c.setNotificationUri(getContext().getContentResolver(), uri);

return c;

}

@Override

public String getType(Uri uri) {

switch (sUriMatcher.match(uri)) {

case NOTES:

case LIVE_FOLDER_NOTES:

return Notes.CONTENT_TYPE;

case NOTE_ID:

return Notes.CONTENT_ITEM_TYPE;

default:

throw new IllegalArgumentException("Unknown URI " + uri);

}

}

有關資料的插入操作,重寫ContentProvider的insert方法即可

@Override

public Uri insert(Uri uri, ContentValues initialValues) {

if (sUriMatcher.match(uri) != NOTES) {

throw new IllegalArgumentException("Unknown URI " + uri);

}

ContentValues values;

if (initialValues != null) {

values = new ContentValues(initialValues);

} else {

values = new ContentValues();

}

Long now = Long.valueOf(System.currentTimeMillis());

if (values.containsKey(NotePad.Notes.CREATED_DATE) == false) {

values.put(NotePad.Notes.CREATED_DATE, now);

}

if (values.containsKey(NotePad.Notes.MODIFIED_DATE) == false) {

values.put(NotePad.Notes.MODIFIED_DATE, now);

}

if (values.containsKey(NotePad.Notes.TITLE) == false) {

Resources r = Resources.getSystem();

values.put(NotePad.Notes.TITLE, r.getString(android.R.string.untitled));

}

if (values.containsKey(NotePad.Notes.NOTE) == false) {

values.put(NotePad.Notes.NOTE, "");

}

SQLiteDatabase db = mOpenHelper.getWritableDatabase();

long rowId = db.insert(NOTES_TABLE_NAME, Notes.NOTE, values);

if (rowId > 0) {

Uri noteUri = ContentUris.withAppendedId(NotePad.Notes.CONTENT_URI, rowId);

getContext().getContentResolver().notifyChange(noteUri, null); //通知資料庫內容有改變

return noteUri;

}

throw new SQLException("Failed to insert row into " + uri);

}

@Override

public int delete(Uri uri, String where, String[] whereArgs) {

SQLiteDatabase db = mOpenHelper.getWritableDatabase();

int count;

switch (sUriMatcher.match(uri)) {

case NOTES:

count = db.delete(NOTES_TABLE_NAME, where, whereArgs);

break;

case NOTE_ID:

String noteId = uri.getPathSegments().get(1);

count = db.delete(NOTES_TABLE_NAME, Notes._ID + "=" + noteId

+ (!TextUtils.isEmpty(where) ? " AND (" + where + ')' : ""), whereArgs);

break;

default:

throw new IllegalArgumentException("Unknown URI " + uri);

}

getContext().getContentResolver().notifyChange(uri, null);

return count;

}

@Override

public int update(Uri uri, ContentValues values, String where, String[] whereArgs) {

SQLiteDatabase db = mOpenHelper.getWritableDatabase();

int count;

switch (sUriMatcher.match(uri)) {

case NOTES:

count = db.update(NOTES_TABLE_NAME, values, where, whereArgs);

break;

case NOTE_ID:

String noteId = uri.getPathSegments().get(1);

count = db.update(NOTES_TABLE_NAME, values, Notes._ID + "=" + noteId

+ (!TextUtils.isEmpty(where) ? " AND (" + where + ')' : ""), whereArgs);

break;

default:

throw new IllegalArgumentException("Unknown URI " + uri);

}

getContext().getContentResolver().notifyChange(uri, null);

return count;

}

最後我們需要在構造奔雷時就監聽Uri,如果處理的Uri需要其他程式獲知,需要在Androidmanifest.xml檔案中顯式的匯出provider的Uri定義

static {

sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);

sUriMatcher.addURI(NotePad.AUTHORITY, "notes", NOTES);

sUriMatcher.addURI(NotePad.AUTHORITY, "notes/#", NOTE_ID);

sUriMatcher.addURI(NotePad.AUTHORITY, "live_folders/notes", LIVE_FOLDER_NOTES);

sNotesProjectionMap = new HashMap();

sNotesProjectionMap.put(Notes._ID, Notes._ID);

sNotesProjectionMap.put(Notes.TITLE, Notes.TITLE);

sNotesProjectionMap.put(Notes.NOTE, Notes.NOTE);

sNotesProjectionMap.put(Notes.CREATED_DATE, Notes.CREATED_DATE);

sNotesProjectionMap.put(Notes.MODIFIED_DATE, Notes.MODIFIED_DATE);

// Support for Live Folders.

sLiveFolderProjectionMap = new HashMap();

sLiveFolderProjectionMap.put(LiveFolders._ID, Notes._ID + " AS " +

LiveFolders._ID);

sLiveFolderProjectionMap.put(LiveFolders.NAME, Notes.TITLE + " AS " +

LiveFolders.NAME);

// Add more columns here for more robust Live Folders.

}

}

最後安博中程網站幫助初學者簡單的概括下Android的ContentProvider類的作用,就是儘可能的減少SQL語句的編寫在外部操作,封裝成方法,而有關SQL語言的執行在DatabaseHelper中也被簡化和分離出了,而SQL語句主要是體現在選擇表的欄位,where這樣的條件限定語句大大減少了我們日常的開發。

相關文章