public class PersonProvider extends ContentProvider {
//创建工具类实现Uri匹配
private static final UriMatcher MATCHER = new UriMatcher(UriMatcher.NO_MATCH);
private static final int PERSONS = 1;
private static final int PERSON = 2;
static{
MATCHER.addURI("cn.test.providers.personprovider", "person", PERSONS);
MATCHER.addURI("cn.test.providers.personprovider", "person/#", PERSON);
}
private DBOpenHelper dbOpenHelper = null;
@Override
public boolean onCreate() {
dbOpenHelper = new DBOpenHelper(getContext());
return true;
}
@Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
SQLiteDatabase db = dbOpenHelper.getReadableDatabase();
switch (MATCHER.match(uri)) {
case PERSONS: // 获取person表中的所有数据 /person
return db.query("person", projection, selection, selectionArgs, null, null, sortOrder);
case PERSON: // 获取person表中的指定id的数据 /person/20
long id = ContentUris.parseId(uri);
String where = "personid="+ id;
if(selection!=null && !"".equals(selection.trim())){
where += " and "+ selection;
}
return db.query("person", projection, where, selectionArgs, null, null, sortOrder);
default:
throw new IllegalArgumentException("Unknown Uri:"+ uri);
}
}
@Override
public String getType(Uri uri) {
// TODO Auto-generated method stub
return null;
}
@Override
public Uri insert(Uri uri, ContentValues values) {
//取得数据库操作实例
SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
switch (MATCHER.match(uri)) {
case PERSONS:
//执行添加,返回行号,如果主健字段是自增长的,那么行号会等于主键id
long rowid = db.insert("person", "name", values);
//得到拼好的uri
Uri insertUri = ContentUris.withAppendedId(uri, rowid);
//发出数据变化通知(person表的数据发生变化)
getContext().getContentResolver().notifyChange(uri, null);
return insertUri;
default:
//不能识别uri
throw new IllegalArgumentException("Unknown Uri:"+ uri);
}
}
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
//受影响的行数
int num = 0;
switch (MATCHER.match(uri)) {
case PERSONS: // 删除person表中的所有数据 /person
num = db.delete("person", selection, selectionArgs);
break;
case PERSON: // 删除person表中的指定id的数据 /person/20
long id = ContentUris.parseId(uri);
String where = "personid="+ id;
if(selection!=null && !"".equals(selection.trim())){
where += " and "+ selection;
}
num = db.delete("person", where, selectionArgs);
break;
default:
throw new IllegalArgumentException("Unknown Uri:"+ uri);
}
return num;
}
@Override
public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
int num = 0;
switch (MATCHER.match(uri)) {
case PERSONS: // 更新person表中的所有数据 /person
num = db.update("person", values, selection, selectionArgs);
break;
case PERSON: // 更新person表中的指定id的数据 /person/20
long id = ContentUris.parseId(uri);
String where = "personid="+ id;
if(selection!=null && !"".equals(selection.trim())){
where += " and "+ selection;
}
num = db.update("person", values, where, selectionArgs);
break;
default:
throw new IllegalArgumentException("Unknown Uri:"+ uri);
}
return num;
}
}
public class AccessContentProiderTest extends AndroidTestCase {
public void testInsert() throws Throwable{
ContentResolver resolver = getContext().getContentResolver();
Uri uri = Uri.parse("content://cn.test.providers.personprovider/person");
ContentValues values = new ContentValues();
values.put("name", "lili");
values.put("phone", "110");
values.put("amount", "3000000000");
resolver.insert(uri, values);
}
public void testDelete() throws Throwable{
ContentResolver resolver = getContext().getContentResolver();
Uri uri = Uri.parse("content://cn.test.providers.personprovider/person");
int num =resolver.delete(uri, null, null);
}
public void testUpdate() throws Throwable{
ContentResolver resolver = getContext().getContentResolver();
Uri uri = Uri.parse("content://cn.test.providers.personprovider/person/65");
ContentValues values = new ContentValues();
values.put("amount", 500);
resolver.update(uri, values, null, null);
}
public void testQuery() throws Throwable{
ContentResolver resolver = getContext().getContentResolver();
Uri uri = Uri.parse("content://cn.test.providers.personprovider/person/65");
Cursor cursor = resolver.query(uri, null, null, null, "personid asc");
while(cursor.moveToNext()){
String name = cursor.getString(cursor.getColumnIndex("name"));
Log.i("AccessContentProviderTest", name);
}
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有