关于Android sqlite3数据库创建使用内容提供者contentProvider及解析者ContentResolver,观察者ContentResolver案例



数据库创建

package com.jms.rbclient.db;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

/**
 * Created by liying on 2016/12/27.
 */

public class ProductDBOpenHelper extends SQLiteOpenHelper {
    public ProductDBOpenHelper(Context context) {
        super(context, "product.db", null, 1);

    }

    /**
     * userid 用户id
     * productid 产品id
     * prodNum  数量id
     * proPropertyid 属性id
     * @param db
     */
    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("create table proinfo (_id integer primary key autoincrement,userid varchar(20),productid varchar(20),prodNum varchar(20),proPropertycolorid varchar(20),proPropertysizeid varchar(20))");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }
}
Androidmanifest节点配置
<provider
            android:name=".db.ContentProvider.CarContentProvider"
            android:authorities="com.jms.rbclient"
            android:enabled="true"
            android:exported="true"/>
内容提供者主要代码
package com.jms.rbclient.db.ContentProvider;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;

import com.jms.rbclient.db.ProductDBOpenHelper;

public class CarContentProvider extends ContentProvider {

    private static final int SUCCESS = 1;

    static UriMatcher mUriMatcher=new UriMatcher(UriMatcher.NO_MATCH);

    static {

        mUriMatcher.addURI("com.jms.rbclient","proinfo",SUCCESS);
    }

内容提供者 删除数据

    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {
        // Implement this to handle requests to delete one or more rows.

         int code = mUriMatcher.match(uri);
        if(code==SUCCESS){
            ProductDBOpenHelper helper=new ProductDBOpenHelper(getContext());
            final SQLiteDatabase db = helper.getWritableDatabase();
            db.delete("proinfo",selection,selectionArgs);
            getContext().getContentResolver().notifyChange(uri,null);
        }else{

        throw new UnsupportedOperationException("Not yet implemented");
        }
        return 0;
    }

    @Override
    public String getType(Uri uri) {
        // at the given URI.
        throw new UnsupportedOperationException("Not yet implemented");
    }

    @Override
    public Uri insert(Uri uri, ContentValues values) {

        final int code = mUriMatcher.match(uri);
        if(code==SUCCESS){
            ProductDBOpenHelper helper=new ProductDBOpenHelper(getContext());
            final SQLiteDatabase db = helper.getWritableDatabase();
            db.insert("proinfo",null,values);
            getContext().getContentResolver().notifyChange(uri,null);
        }else{

        throw new UnsupportedOperationException("Not yet implemented");
        }
        return null;

    }

    @Override
    public boolean onCreate() {
        return false;
    }

    @Override
    public Cursor query(Uri uri, String[] projection, String selection,
                        String[] selectionArgs, String sortOrder) {

        final int code = mUriMatcher.match(uri);
        if(code==SUCCESS){
            ProductDBOpenHelper helper=new ProductDBOpenHelper(getContext());
            final SQLiteDatabase db = helper.getWritableDatabase();
            final Cursor cursor = db.query("proinfo", projection, selection, selectionArgs, null, null, sortOrder);
            getContext().getContentResolver().notifyChange(uri,null);
            return  cursor;
        }else{

        throw new UnsupportedOperationException("Not yet implemented");
        }

    }

    @Override
    public int update(Uri uri, ContentValues values, String selection,
                      String[] selectionArgs) {
        final int code = mUriMatcher.match(uri);
        if(code==SUCCESS){
            ProductDBOpenHelper helper=new ProductDBOpenHelper(getContext());
            final SQLiteDatabase db = helper.getWritableDatabase();
            db.update("proinfo",values,selection,selectionArgs);
            getContext().getContentResolver().notifyChange(uri,null);
        }else{

        throw new UnsupportedOperationException("Not yet implemented");
        }
        return 0;
    }

}


代码工具封装  这里封装的是关于购物的,并不广泛适用

package com.jms.rbclient.db.ContentProvider;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;

/**
 * Created by liying on 2016/12/27.
 */

public class DBOUtils {


    private Context mContext;
    /**
     * 在构造方法里面初始化helper对象.
     *
     * @param context
     */
    public DBOUtils(Context context) {

        mContext = context;
    }
    /**
     * String userid;       // 用户id
     * String productid;     // 产品id
     * int prodNum;        // 数量
     * int proPropertyidcolor;  // 属性颜色id
     * int proPropertyidsize;  // 属性尺寸id
     */

    public void insert(String userid,String productid,int prodNum,int proPropertyidcolor,int proPropertyidsize) {

        final ContentResolver resolver = mContext.getContentResolver();
        Uri uri = Uri.parse("content://com.itheima.rbclient/proinfo");
        ContentValues values = new ContentValues();
        String userid1=userid;               // 用户id
        String productid1=productid;          // 产品id
        String prodNum1=prodNum+"";                 // 数量
        String proPropertyidcolor1=proPropertyidcolor+"";       // 属性颜色id
        String proPropertyidsize1=proPropertyidsize+"";      // 属性尺寸id
       values.put("userid",userid1);
       values.put("productid",productid1);
       values.put("prodNum",prodNum1);
       values.put("proPropertycolorid",proPropertyidcolor1);
       values.put("proPropertysizeid",proPropertyidsize1);
        resolver.insert(uri, values);

    }
    /**
     * userid 用户id
     * productid 产品id
     * prodNum  数量id
     * proPropertyid 属性id
     *
     */
    /**
     * 删除数据 具备 产品id     数量id     属性id
     */
    public void deleteSingle(String userid,String productid,int proPropertyidcolor,int proPropertyidsize){
        final ContentResolver resolver = mContext.getContentResolver();
        Uri uri=Uri.parse("content://com.jms.rbclient/proinfo");
        String userid1=userid;               // 用户id
        String productid1=productid;          // 产品id
        String proPropertyidcolor1=proPropertyidcolor+"";       // 属性颜色id
        String proPropertyidsize1=proPropertyidsize+"";      // 属性尺寸id
        String where="userid=?,productid=?,proPropertyidcolor=?,proPropertyidsize=?";
        String[] selectionArgs=new String[]{userid1,productid1,proPropertyidcolor1,proPropertyidsize1};
        resolver.delete(uri,where,selectionArgs);
    }
    /**
     * 清空数据 具备 产品id     数量id     属性id
     */
    public void deleteAll(String userid){
        final ContentResolver resolver = mContext.getContentResolver();
        Uri uri=Uri.parse("content://com.jms.rbclient/proinfo");
        String userid1=userid;               // 用户id
        String where="userid=?";
        String[] selectionArgs=new String[]{userid1};
        resolver.delete(uri,where,selectionArgs);
    }

    /**更新数据*/
    public void update(String userid,String productid,int proPropertyidcolor,int proPropertyidsize,int newnum,int newcolor,int newsize) {

        final ContentResolver resolver = mContext.getContentResolver();
        Uri uri = Uri.parse("content://com.jms.rbclient/proinfo");
        ContentValues values = new ContentValues();
        String newnum1=newnum+"";               // 用户id
        String newcolor1=newcolor+"";               // 属性颜色id
        String newsize1=newsize+"";               // 属性尺寸id
        /*--------------- add begin ---------------*/
        String userid1=userid;               // 用户id
        String productid1=productid;          // 产品id
        String proPropertyidcolor1=proPropertyidcolor+"";       // 属性颜色id
        String proPropertyidsize1=proPropertyidsize+"";      // 属性尺寸id
        /*--------------- add begin ---------------*/
        values.put("prodNum",newnum1);
        values.put("proPropertycolorid",newcolor1);
        values.put("proPropertysizeid",newsize1);
/*--------------- add begin ---------------*/

        String where="userid=?,productid=?,proPropertyidcolor=?,proPropertyidsize=?";
        String[] selectionArgs=new String[]{userid1,productid1,proPropertyidcolor1,proPropertyidsize1};
        resolver.update(uri, values,where,selectionArgs);

    }
    public Cursor query(String userid, String productid, int proPropertyidcolor, int proPropertyidsize){
        final ContentResolver resolver = mContext.getContentResolver();
        Uri uri=Uri.parse("content://com.jms.rbclient/proinfo");
        String userid1=userid;               // 用户id
        String productid1=productid;          // 产品id
        String proPropertyidcolor1=proPropertyidcolor+"";       // 属性颜色id
        String proPropertyidsize1=proPropertyidsize+"";      // 属性尺寸id
        String selection="userid=?,productid=?,proPropertyidcolor=?,proPropertyidsize=?";
        String[] selectionArgs=new String[]{userid1,productid1,proPropertyidcolor1,proPropertyidsize1};
       return resolver.query(uri,null,selection,selectionArgs,null);
    }
    public Cursor queryAll(String userid){
        final ContentResolver resolver = mContext.getContentResolver();
        Uri uri=Uri.parse("content://com.jms.rbclient/proinfo");
        String userid1=userid;               // 用户id
        String selection="userid=?";
        String[] selectionArgs=new String[]{userid1};
       return resolver.query(uri,null,selection,selectionArgs,null);
    }
}
fragment 内容观察者监听回调

 mActivity.getContentResolver().registerContentObserver(uri, true, new ContentObserver(new Handler()) {
            @Override
            public void onChange(boolean selfChange) {
                Toast.makeText(getContext(),"数据有变化",Toast.LENGTH_SHORT).show();
                refreshUI();
                super.onChange(selfChange);
            }
        });

以上为之前项目中Android本地数据库的相关代码,同行攻城狮们有建议,请不吝赐教哦!




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值