ContentProvider与数据库结合使用

package practise.lxm.myProvider;

import android.net.Uri;
import android.provider.BaseColumns;

/**
 * Created by Administrator on 2015/4/9.
 * 工具类,提供给调用该ContentProvider的ContentResolver
 */
public class Words {

    public static final String AUTHORITY = "lxm.practise.provide";
    //单词表列名类
    public static class Word implements BaseColumns{
        //允许操作的列
        public static final String _ID = "_id";
        public static final String WORD = "word";
        public static final String DETAIL = "detail";
        //提供的两个uri
        public static final Uri ALL_WORDS = Uri.parse("content://" + AUTHORITY + "/words");
        public static final Uri ONE_WORD = Uri.parse("content://" + AUTHORITY + "/word");
    }

}


package practise.lxm.myProvider;

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;

/**
 * Created by Administrator on 2015/4/9.
 */
public class MyProvider extends ContentProvider {

    public static final String TABLE_NAME= "words";
    //操作数据库
    private MySQLiteOpenDataBase dbHelper ;
    //UriMatcher
    private static UriMatcher uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
    //注册代码
    private static final int ALL_WORDS_CODE = 1;
    private static final int WORDS_ID_CODE = 2;

    static {
        uriMatcher.addURI(Words.AUTHORITY,"words",ALL_WORDS_CODE);
        uriMatcher.addURI(Words.AUTHORITY,"word/#",WORDS_ID_CODE);
    }
    @Override
    public boolean onCreate() {
        dbHelper = new MySQLiteOpenDataBase(this.getContext(),"MyProvider.db3",1);
        return true;
    }

    //查询
    @Override
    public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
        SQLiteDatabase db = dbHelper.getReadableDatabase(); //获取数据库操作对象
        switch (uriMatcher.match(uri)){ //查询所有
            case ALL_WORDS_CODE:
                return db.query(TABLE_NAME,projection,selection,selectionArgs,null,null,sortOrder);
            case WORDS_ID_CODE: //根据id查询
                long id= ContentUris.parseId(uri);
                String wordsWhere = Words.Word._ID + " =" + id;
                if(selection != null && selection.length() > 0){
                    wordsWhere += " and " + selection;
                }
                return db.query(TABLE_NAME,projection,wordsWhere,selectionArgs,null,null,sortOrder);
            default: //未知
                throw new IllegalArgumentException("未知的Uri:" + uri.toString());

        }
    }

    //获取类型
    @Override
    public String getType(Uri uri) {
        switch (uriMatcher.match(uri)){
            case ALL_WORDS_CODE: //多项
                return "vnd.android.cursor.dir/practise.lxm.myProvider.Word";
            case WORDS_ID_CODE: //单项
                return "vnd.android.cursor.item/practise.lxm.myProvider.Word";
            default: //未知
                throw new IllegalArgumentException("未知的Uri:" + uri.toString());
        }
    }

    //新增
    @Override
    public Uri insert(Uri uri, ContentValues values) {
        SQLiteDatabase db = dbHelper.getWritableDatabase();//获取操作数据库对象
        switch (uriMatcher.match(uri)){
            case ALL_WORDS_CODE:
                //新增到数据库
                long row_id = db.insert(TABLE_NAME,null,values);
                if(row_id > 0) { //新增成功
                    Uri newUri = ContentUris.withAppendedId(uri, row_id);
                    //通知数据已改变
                    getContext().getContentResolver().notifyChange(newUri,null);
                    return newUri;
                }
                break;
            default: //未知
                throw new IllegalArgumentException("未知的Uri:" + uri.toString() + " code:" + uriMatcher.match(uri));
        }
        return null;
    }

    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {
        SQLiteDatabase db = dbHelper.getReadableDatabase();//获取操作数据库对象
        int delCount = 0; //删除的数量
        switch (uriMatcher.match(uri)){
            case WORDS_ID_CODE: //单项
                //添加删除条件,id为指定值
                long delId = ContentUris.parseId(uri);
                String delWhere = Words.Word._ID + "=" + delId;
                if(selection != null && selection.length() > 0){
                    delWhere += " and " + selection;
                }
                //执行删除
                delCount = db.delete(TABLE_NAME,delWhere,selectionArgs);
                break;
            case ALL_WORDS_CODE: //删除多项
                delCount = db.delete(TABLE_NAME,selection,selectionArgs);
            default: //未知
                throw new IllegalArgumentException("未知的Uri:" + uri.toString());
        }
        //通知数据已更改
        getContext().getContentResolver().notifyChange(uri,null);
        return delCount;
    }

    @Override
    public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
        SQLiteDatabase db = dbHelper.getWritableDatabase();//获取操作数据库对象
        int updateCount = 0; //更新的数量
        switch (uriMatcher.match(uri)){
            case WORDS_ID_CODE: //单项
                //添加修改条件,id为指定值
                long updateId = ContentUris.parseId(uri);
                String updateWhere = Words.Word._ID + "=" + updateId;
                if(selection != null && selection.length() > 0){
                    updateWhere += " and " + selection;
                }
                //执行删除
                updateCount = db.update(TABLE_NAME,values, updateWhere,selectionArgs);
                break;
            case ALL_WORDS_CODE: //删除多项
                updateCount = db.update(TABLE_NAME,values, selection,selectionArgs);
            default: //未知
                throw new IllegalArgumentException("未知的Uri:" + uri.toString());
        }
        //通知数据已更改
        getContext().getContentResolver().notifyChange(uri,null);
        return updateCount;
    }
}


你可能感兴趣的:(android数据共享)