public class PersonProvider extends ContentProvider {
private static final UriMatcher MATCHER = new UriMatcher(UriMatcher.NO_MATCH);
private static final int PERSON = 1;
private static final int PERSON2 = 2;
static {
MATCHER.addURI("cn.com.providers.personprovider", "person", PERSON);
MATCHER.addURI("cn.com.providers.personprovider", "person/#", PERSON2);
}
private DBOpenHelper dbOpenHelper=null;
//删除
public int delete(Uri uri, String selection, String[] selectionArgs) {
SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
int num = 0;
switch (MATCHER.match(uri)) {
case PERSON:
num = db.delete("person", selection, selectionArgs);
break;
case PERSON2:
long id = ContentUris.parseId(uri);
String where = " personid = "+id;
if(selection!=null&&!"".equals(selection.trim())){
where = where + " and "+selection;
}
db.delete("person", where, selectionArgs);
break;
default:
throw new IllegalArgumentException("uri 不合法");
}
return num;
}
public String getType(Uri uri) {
switch (MATCHER.match(uri)) {
case PERSON:
return "vnd.android.cursor.dir/person";
case PERSON2:
return "vnd.android.cursor.item/person";
default:
throw new IllegalArgumentException("uri err!");
}
}
//增加
public Uri insert(Uri uri, ContentValues values) {
SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
switch (MATCHER.match(uri)) {
case PERSON:
long rowid = db.insert("person", "name", values);
Uri insertUri = ContentUris.withAppendedId(uri, rowid);
return insertUri;
default:
throw new IllegalArgumentException("uri 不合法");
}
}
public boolean onCreate() {
dbOpenHelper = new DBOpenHelper(getContext());
return true;
}
public Cursor query(Uri uri, String[] projection, String selection,String[] selectionArgs, String sortOrder) {
SQLiteDatabase db = dbOpenHelper.getReadableDatabase();
switch (MATCHER.match(uri)) {
case PERSON:
return db.query("person", projection, selection,selectionArgs, null, null, sortOrder);
case PERSON2:
long id = ContentUris.parseId(uri);
String where = " personid = "+id;
if(selection!=null&&!"".equals(selection.trim())){
where = where + " and "+selection;
}
return db.query("person", projection, where,selectionArgs, null, null, sortOrder);
default:
throw new IllegalArgumentException("uri error@!");
}
}
//更新
public int update(Uri uri, ContentValues values, String selection,String[] selectionArgs) {
SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
int num = 0;
switch (MATCHER.match(uri)) {
case PERSON:
num = db.update("person", values, selection, selectionArgs);
break;
case PERSON2:
long id = ContentUris.parseId(uri);
String where = " personid = "+id;
if(selection!=null&&!"".equals(selection.trim())){
where = where +" and "+selection;
}
num =db.update("person", values, where, selectionArgs);
break;
default:
throw new IllegalArgumentException("uri非法");
}
return num;
}
}
package cn.itcast.test;
public class AccessContentProviderTest extends AndroidTestCase {
public void testInsert() throws Throwable{
ContentResolver resolver = getContext().getContentResolver();
Uri uri = Uri.parse("content://cn.com.providers.personprovider/person");
ContentValues values = new ContentValues();
values.put("name", "lili");
values.put("phone", "110");
values.put("amount", "1000000");
for(int i = 0;i<20;i++){
resolver.insert(uri, values);
}
}
public void testDelete()throws Throwable{
ContentResolver resolver=getContext().getContentResolver();
Uri uri = Uri.parse("content://cn.com.providers.personprovider/person/403");
resolver.delete(uri, null, null);
}
public void testUpdata()throws Throwable{
ContentResolver resolver= getContext().getContentResolver();
Uri uri= Uri.parse("content://cn.com.providers.personprovider/person/404");
ContentValues values = new ContentValues();
values.put("amount", 25);
resolver.update(uri, values, null, null);
}
public void testQuery() throws Throwable{
ContentResolver resolver = getContext().getContentResolver();
Uri uri = Uri.parse("content://cn.com.providers.personprovider/person");
Cursor cursor = resolver.query(uri, null, null, null, "personid asc");
while(cursor.moveToNext()){
String name = cursor.getString(cursor.getColumnIndex("name"));
Log.i("AccessContentProviderTest", name);
}
}
}