Package cn. class3g. db; Import cn. class3g. service. DatabaseHelper; Import android. content. ContentProvider; Import android. content. ContentUris; Import android. content. ContentValues; Import android. content. UriMatcher; Import android. database. Cursor; Import android. database. sqlite. SQLiteDatabase; Import android.net. Uri; Public class PersonProvider extends ContentProvider { Private static UriMatcher matcher = new UriMatcher (UriMatcher. NO_MATCH ); Private static final int PERSONS = 1; Private static final int PERSON = 2; Private DatabaseHelper dbHelper; Static { Matcher. addURI ("cn. class3g. providers. personprovider", "person", PERSONS ); Matcher. addURI ("cn. class3g. providers. personprovider", "person /#", PERSON ); } Public boolean onCreate (){ DbHelper = new DatabaseHelper (this. getContext ()); Return true; } // Content: // cn. itcast. provides. personprovider/person Public Uri insert (Uri uri, ContentValues values ){ SQLiteDatabase db = dbHelper. getWritableDatabase (); Long rowId; Switch (matcher. match (uri )){ Case PERSONS: // Add a new record to the table and return its row number RowId = db. insert ("person", "personid", values ); Return ContentUris. withAppendedId (uri, rowId ); Default: Throw new IllegalArgumentException ("Unknow Uri:" + uri ); } } Public Cursor query (Uri uri, String [] projection, String selection, String [] selectionArgs, String sortOrder ){ SQLiteDatabase db = dbHelper. getReadableDatabase (); Switch (matcher. match (uri )){ Case PERSONS: Return db. query ("person", projection, selection, selectionArgs, null, null, sortOrder ); Case PERSON: Long personid = ContentUris. parseId (uri ); String where = "personid =" + personid; If (selection! = Null &&! "". Equals (selection )){ Where = where + "and" + selection; } Return db. query ("person", projection, where, selectionArgs, null, null, sortOrder ); Default: Throw new IllegalArgumentException ("Unknown Uri:" + uri ); } } // Content: // CN. itcast. Provides. personprovider/person update all records in the table // Content: // CN. itcast. Provides. personprovider/person/10 update the record of the specified ID in the table Public int Update (URI Uri, contentvalues values, string selection, String [] selectionargs ){ Sqlitedatabase DB = dbhelper. getwritabledatabase (); Int num; Switch (matcher. match (uri )){ Case PERSONS: // update a specified record Num = db. update ("person", values, selection, selectionArgs ); Break; Case PERSON: Long personid = ContentUris. parseId (uri ); String where = "personid =" + personid; If (selection! = Null ){ Where + = "and" + selection; } Num = db. update ("person", values, where, selectionArgs ); Break; Default: Throw new IllegalArgumentException ("Unknow Uri" + uri ); } Return num; } Public int delete (Uri uri, String selection, String [] selectionArgs ){ SQLiteDatabase db = dbHelper. getWritableDatabase (); Int num = 0; Switch (matcher. match (uri )){ Case PERSONS: Num = db. delete ("person", selection, selectionArgs ); Break; Case PERSON: Long personid = ContentUris. parseId (uri ); String where = "personid =" + personid; If (selection! = Null &&! "". Equals (selection )){ Where = where + "and" + selection; } Num = db. delete ("person", where, selectionArgs ); Break; Default: Throw new IllegalArgumentException ("Unknown Uri:" + uri ); } Return num; } Public String getType (Uri uri ){ Switch (matcher. match (uri )){ Case PERSONS: Return "vnd. android. cursor. dir/person "; Case PERSON: Return "vnd. android. cursor. item/person "; Default: Throw new IllegalArgumentException ("Unknown Uri:" + uri ); } } } |