Info

DiaryContentProvider MftMm^ty, mmW?

it^m^mnfexr DatabaseHelper ttilftg. 8 tf'PWiiflHWitt

private static class DatabaseHelper extends SQLiteOpenHelper \ DatabaseHelper(Context context) {

super(context, DATABASE NAME, null, DATA3A5E_V2RSI0N); }

(¡»Override public void onCreate (SQI.i teDatabase db) {

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

+ DiaryColumns. ID + " INTEGER PRIMARY KEY,"

♦ DiaryColumns.TITLE ♦ " TEXT," f DiaryColumns.BODY

♦ " TEXT," 4 DiaryColumns.CREATED + " TEXT" + ");"); )

^Override public void onUpgrade(SQLiteDatabase db, xnt oldVersion, int newVersion) { db.execSQL("DROP TABLE IF EXISTS notes"); onCreate(db); ) )

DatabaseHelper ^m^WS-WMim, »iiS'NSftff« IIifc^E^»««-:.

& DiaryContentProvider »1«, mttfcX 7-^«tiMfrJ». X'I'imm\ k&&m3&9Mmif!iil&*

private static final String DATABASE NAME = "database"; private static final int DATABASE VERSION 1; private static final String DIARY^TABLE NAME = "diary";

private static HashMap<String, String> sD.iariesPro jectionMap;

private static final int DIARIES = 1; private static final int DIARY ID = 2;

private static final UriMatcher sUriMatcher; if&M: Uri Matcher?

Uri Matcher xt K Uri ft - ^ £ , ffltP, ^ ft ill ft DiaryContentProvider /'hfcHR : + ft static + , slJr iMatcher = new UriMatcher (UnMatcher .NO MATCH) ; sUriMatcher.addURI(Diary.AUTHORITY, "diaries", DIARIES); sUr iMatcheit . addURI (Diary . AUTHORITY, "diaries/it ", DIARY_ID) ;

• sUriMatcher.addURl( Diary. AUTHORITY, "diaries", sUriMatcher.match(uri))&/}<> iP&ffiff'JW Uri content://com.ex09 2 contentprovider.diarycontentprovider/diaries/. ^ sUriMatcher.match(uri) WiSlEliftiitfi DIARIES«

• sUriMatcher.addURI(Diary.AUTHORITY, "diaries///", DIARY ID)^/j<, toSMJW Uri & content://com.ex09 2 contentprovider.diarycontentprovider/diaries/id (K^friizM id ii -^Mfr'/)* W & sUri Matcher. match( uri l"l ill «S fk DIARY ID, ifflH UriMatcher J nfiUmUri fttfSffi, Uri J

(1) insertOo

Frr/Tc:

@0verride public Uri insert (Uri uri, ContentValues nitialValues) { if (sUriMatcher.match(uri) != DIARIES) (

throw new IllegalArgumentExcept. ion ("Unknown URI " + uri); )

ContentValues values;

values = new ContentValues(initialValues);

values = new ContentValues(); I

if (values.containsKey(Diary.DiaryColumns.CREATED) false) {

values . put. ( Diary . DiaryColumns .CREATED, get FormateCreatedDate () ) ;

if (values.containsKey(Diary.DiaryColumns.TITLE) == false) ( Resources r Resources.getSystem(); values.put(Diary.DiaryColumns.TITLE, r .getstring(Android.R.string.untitled)) ; )

if (values.containsKey(Diary.DiaryColumns.BODY) - false) ( values.put(Diary.DiaryColumns.BODY, ""); 1

SQLiteDatabase db = mOpenHelper.getWritableDatabase();

long rowld = db.insert(DIARY_TABLE_NAME, DiaryColumns.BODY, values);

Uri diaryUri ContentUris.withAppendedld( Diary.DiaryColumns.CONTENT URI, rowld); return diaryUri;

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

• ft ftiMMMiSiO sUriMatcher.match(uri) != DIARIES rtVMMttJ Uri ffltr fHW\* SS t Uri DIARIES Urio

• SQLiteDatabase db = mOpenHelper.getWritableDatabase()i/H'J1;i#ff$lJ -f* SQLiteDatabase

• db.insert (DIARY TABLE NAME, DiaryColumns.BODY, values) L/ii'JftffitMiA -^id^i'J

• «SSjKMii. insert()i&NW^ ^ Uri. 'tid^Mid, ffrW, JMJ&^&ftlitU ft id ftjiuJS t- Uri: Uri diaryUri- ContentUris. withAppcndedId( Diary. Diary Col umns.CONTENT URI, row I d). w i th A ppended I d() Jj iA {h FiifKl'hfc WR^itM^Mo

: ContentUris £ content URJ ftttfPS^iMMMi. JMfriP

• public static Uri withA ppended Id ( Uri contentUri, long id), // ii ft w tti id ill contentUri - --iNSiM Urio LtiUftiMJiX-tW r-a + fiS^afli»: ContentUris.withAppendedld (Diary.

DiaryColumns.CONTENT URI, rowld K in ^ rowld 100 , ^SftfrMi*^ Uri 1% ft 'ft content://com.ex09 2 contentprovider.diarycontentprovider/diaries/lOOc

• public static long parse Id ( Uri contentUri), f- /jtL ft "jJHE content IJRl Jgj&ift idMt/iMV^. tb'iinfltft.ii Is* content URI ii content://com.ex09 2 contentprovider.diarycontentprovider/diaries/100,

Jl^a^ifittWjEHittlltJi iooo

@Override public int delete(Uri uri, String where, String IJ whereArgs) i SQLiteDatabase db = mOpenHelper.getWritableDatabase(); String rowld = uri.getPathSegments().get(I);

return db . del et.e (DIARY TABLE NAME, DiaryCoiuirins ._ID * " = " + rowld, null); )

• rowld = uri.getPathSegments().get( 1 )jj\ Mffl$'J rowld Klito getPathSegmentsOAvi^fiJ—t- String fft List, uri.gctPathSegments().get( 1 )>J

rowld, uri.getPathSegments().get(0)IPifiM^MdiariesMo

• db.delete(DIARY TABLE NAME, DiaryColumns. ID + "=H + rowld, SQLite M

<3 Over ride public int update(Uri uri, ContentValues values, String where, String!] whereArgs) {

SQLiteDatabase db = mOpenHelper.getWritableDatabase(); String rowld = uri.getPathSegments().get(1);

return db.update(DIARY TABLE NAME, values, DiaryColumns._ID + "="

\ immih

• tt&nm SQLiteDatabase WJCi'&M rowld, ift/5 f? ffl db.update(DIARY_ TABLE NAME, values, DiaryColumns. ID + "="+ rowld, null)ig^iMrli$rrffc.

@Override public Cursor query(Uri uri, Stringt] projection, String selection.

String[] selectionArgs, String sortOrder) {

SQLiteQueryBuilder qb new SQLiteQuery3uilder();

switch (sUriMatcher.match(uri)) { case DIARIES:

qb.setTables(DIARY_TABLE_NAME); break;

case DIARY_ID:

qb.setTables(DIARY_TABLE NAME); qb.appendWhere(DiaryColamns.ID t W-H + uri.getPathSegments().get(1)); break;

default:

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

String order3y;

if (TextUtils.isEmpty(sortOrder)) {

orderBy Diary.DiaryCoLumns.DEFAULT SORTORDER;

orderBy = sortOrder;

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

• SQLiteQuery Builder & -^l^ift SQL fStittift'iOMtttt^.

• sUriMatcher.match(uri), ttMWfiiUc^rlJW^irt, id^ftffio

• DIARIES, »^Uffifitt.if qb.setTables (DIARY TABLE NAME) igfiJsfcnJ W / o

• ftl [<3 £ DIARY JD, IIS^iEffi®» where « » » # » « E 2t * . tt ^ qb.appendWhere( DiaryColumns. ID + "="+ uri.getPathSegments().get( 1))0

• SQLiteDatabase db = mOpen Helper. getReadableDatabase<), -^W&W) SQLiteDatabase

• Cursor c = qb.query ( db, projection, selection, selectionArgs, null,null, orderBy) if\nj, ifij % U T- -tteWfft SQL fHjg&^im^ SQLiteQuery Builder ffD

SQLiteDatabase U&'R&Wh W\ii^^llft^ query (SQLiteDatabase db, Stringl | projectionln, String selection, String[] selectionArgs, String groupBy, String having, String sortOrder, String limit) " K.

• «in]>h#ftj6 -^WTM, MM« -««»frntfiB (?>o

• »A f SQL ^«f M limit ffl^, »iWiSH»®*»^».

£ DiaryContentProvider fi Hi W ffi t ft . -1 & getType ( Uri uri ), W - t & getFormateCreatedDateOo fcXWL&NWmJ

public String getType(Uri uri) { switch (sUriMatcher.match(uri)) { case DIARIES:

return DiaryColumns.CONTENTTYPE; case DIARY_ID:

return DiaryColumns.C0NTENT_ITEM TYPE; default:

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

• jfc/ji&i&M -^rr^ Uri MIME vnd.Android. cursor.item if- ;k. flB&atftiU*^ Uri JRSgfflinSJfeW vnd.Android.cursor.dir »¿»HUS^ Uri »«WJSI^iBftlfio

Was this article helpful?

0 0

Post a comment