URI介绍:
AndroidManifest.xml
在<application>节点下加入
<provider android:name=".PersonProvider" android:authorities="com.lipeng.providers.personprovider" android:exported="true"/>
PersonProvider.java
package com.example.test_sqlite;
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;
import com.example.test_sqlite.service.SQLHelper;
public class PersonProvider extends ContentProvider{
private SQLHelper sqlHelper = null;
private static final UriMatcher MATCHER = new UriMatcher(UriMatcher.NO_MATCH); // NO_MATCH值为-1
private static final int PERSONS = 1;
private static final int PERSON = 2;
private final String TABLE_NAME_PERSON = "person";
static{
MATCHER.addURI("com.lipeng.providers.personprovider", "person", PERSONS);
MATCHER.addURI("com.lipeng.providers.personprovider", "person/#", PERSON);
}
@Override
public boolean onCreate() {
sqlHelper = new SQLHelper(getContext());
return true;
}
@Override
public Uri insert(Uri uri, ContentValues values) {
SQLiteDatabase db = sqlHelper.getWritableDatabase();
switch (MATCHER.match(uri)) {
case PERSONS:
long rowid = db.insert(TABLE_NAME_PERSON, "name", values); // 主键值
Uri insertUri = ContentUris.withAppendedId(uri, rowid);
return insertUri;
default:
throw new IllegalArgumentException("this is Unknow Uri:" + uri);
}
}
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
SQLiteDatabase db = sqlHelper.getWritableDatabase();
int num = 0;
switch (MATCHER.match(uri)) {
case PERSONS:
num = db.delete(TABLE_NAME_PERSON, selection, selectionArgs);
break;
case PERSON:
long rowid = ContentUris.parseId(uri);
String where = "personid=" + rowid;
if(selection != null && !"".equals(selection.trim())){
where += " and " + selection;
}
num = db.delete(TABLE_NAME_PERSON, where, selectionArgs);
break;
default:
throw new IllegalArgumentException("this is Unknow Uri:" + uri);
}
return num;
}
@Override
public int update(Uri uri, ContentValues values, String selection,
String[] selectionArgs) {
SQLiteDatabase db = sqlHelper.getWritableDatabase();
switch (MATCHER.match(uri)) {
case PERSONS:
return db.update(TABLE_NAME_PERSON, values, selection, selectionArgs);
case PERSON:
long rowid = ContentUris.parseId(uri);
String where = "personid=" + rowid;
if(selection != null && !"".equals(selection.trim())){
where += " and " + selection;
}
return db.update(TABLE_NAME_PERSON, values, where, selectionArgs);
default:
throw new IllegalArgumentException("this is Unknow Uri:" + uri);
}
}
/**
* projection 要查询的字段
*/
@Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
// 约定:/person表示查询表的所有数据;/person/10表示查询id为10的数据
SQLiteDatabase db = sqlHelper.getReadableDatabase();
switch (MATCHER.match(uri)) {
case PERSONS:
return db.query(TABLE_NAME_PERSON, projection, selection, selectionArgs, null, null, sortOrder);
case PERSON:
long rowid = ContentUris.parseId(uri);
String where = "personid=" + rowid;
if(selection != null && !"".equals(selection.trim())){
where += " and " + selection;
}
return db.query(TABLE_NAME_PERSON, null, where, selectionArgs, null, null, sortOrder);
default:
throw new IllegalArgumentException("this is Unknow Uri:" + uri);
}
}
@Override
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("this is Unknow Uri:" + uri);
}
}
}
AccessContentProvider.java
package com.example.test;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.test.AndroidTestCase;
import android.util.Log;
public class AccessContentProvider extends AndroidTestCase{
private static final String TAG = "AccessContentProvider";
public void testInsert(){
String uriStr = "content://com.lipeng.providers.personprovider/person";
Uri uri = Uri.parse(uriStr);
ContentResolver resolver = getContext().getContentResolver();
ContentValues values = new ContentValues();
values.put("name", "xxxxxxxxxxxxxx");
values.put("phone", "1311212313");
values.put("amount", "123131");
resolver.insert(uri, values);
}
public void testDelete(){
String uriStr = "content://com.lipeng.providers.personprovider/person/80";
Uri uri = Uri.parse(uriStr);
ContentResolver resolver = getContext().getContentResolver();
// 这里不能再制定personid=?,因为最终会组成例如personid=10 and personid=20
resolver.delete(uri, "name=?", new String[]{"lipeng"});
// 如果什么都不需要指定,只需要设置/person/20,而delete函数后两个参数为null就行
// resolver.delete(uri, null, null);
}
public void testUpdate(){
String uriStr = "content://com.lipeng.providers.personprovider/person/79";
Uri uri = Uri.parse(uriStr);
ContentResolver resolver = getContext().getContentResolver();
ContentValues values = new ContentValues();
values.put("name", "John");
values.put("phone", "131565464532");
values.put("amount", "90000000");
resolver.update(uri, values, null, null);
}
public void testQuery(){
String uriStr = "content://com.lipeng.providers.personprovider/person/70"; // 查询id为70的Person
String uriStr2 = "content://com.lipeng.providers.personprovider/person"; // 查询所有Person
Uri uri = Uri.parse(uriStr2);
ContentResolver resolver = getContext().getContentResolver();
Cursor cursor = resolver.query(uri, null, null, null, "personid asc");
while(cursor.moveToNext()){
String name = cursor.getString(cursor.getColumnIndex("name"));
Log.i(TAG, name);
}
}
}