ContentProvider結合SQLiteDatabase用法

ContentProvider的作用就是很方便的通過uriMatcher匹配一個或多個uri地址,方便以後可以直接通過uri來增刪查改對應的數據,當然還是需要自己在對應的方法中做好處理,貼上代碼實現如下

public class ShopProvider extends ContentProvider {
    public static final String TABLE_NAME = "Shop";
    public static final String AUTHORITY = "com.example.shoppingdemo";
    public static final int SHOP = 1;
    public static final String type = "shop";
    private static final UriMatcher uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
    private static final int version = 1;
    ShoppingDataHelper mHelper;

    public static final String _ID = "_id";
    public static final String TITLE = "name";
    public static final String TYPE = "shoppingtype";
    public static final String DISCRIBUTION = "discribution";
    public static final String IMAGE = "image";


    static{
        uriMatcher.addURI(AUTHORITY, TABLE_NAME, 1);
    }




    @Override
    public boolean onCreate() {
        mHelper = new ShoppingDataHelper(getContext());
        return false;
    }

    @Nullable
    @Override
    public Cursor query(@NonNull Uri uri, @Nullable String[] projection, @Nullable String selection, @Nullable String[] selectionArgs, @Nullable String sortOrder) {
        SQLiteDatabase db = mHelper.getReadableDatabase();
        if(!uriMatched(uri)){
            throw new IllegalArgumentException("bad uri " + uri);
        }
        Cursor c = db.query(TABLE_NAME, projection, selection,selectionArgs, null, null, sortOrder);
        return c;
    }

    @Nullable
    @Override
    public String getType(@NonNull Uri uri) {
        if(!uriMatched(uri)){
            throw new IllegalArgumentException("bad uri " + uri);
        }
        return type;
    }

    @Nullable
    @Override
    public Uri insert(@NonNull Uri uri, @Nullable ContentValues values) {
        if(!uriMatched(uri)){
            throw new IllegalArgumentException("bad uri " + uri);
        }
        SQLiteDatabase db = mHelper.getWritableDatabase();
        long count = db.insert(TABLE_NAME, null, values);
        Uri newUri = ContentUris.withAppendedId(uri, count);
        return newUri;
    }

    @Override
    public int delete(@NonNull Uri uri, @Nullable String selection, @Nullable String[] selectionArgs) {
        if(!uriMatched(uri)){
            throw new IllegalArgumentException("bad uri " + uri);
        }
        SQLiteDatabase db = mHelper.getWritableDatabase();
        int del = db.delete(TABLE_NAME, selection, selectionArgs);
        return del;
    }

    @Override
    public int update(@NonNull Uri uri, @Nullable ContentValues values, @Nullable String selection, @Nullable String[] selectionArgs) {
        if(!uriMatched(uri)){
            throw new IllegalArgumentException("bad uri " + uri);
        }
        SQLiteDatabase db = mHelper.getWritableDatabase();
        int count = db.update(TABLE_NAME, values, selection, selectionArgs);
        return count;
    }

    private boolean uriMatched(Uri uri) {
        return (uriMatcher.match(uri) == SHOP);
    }

SQLiteDatabase是SQLite數據庫相關的數據庫接口,具有增刪查改方法,直接對應書庫處理,現簡單貼上相關實現

class ShoppingDataHelper extends SQLiteOpenHelper {
    private static final String createTable = "create table " + TABLE_NAME + " (_id integer primary key autoincrement," +
            "name text, shoppingtype text, discribution text, image blob  )";

    public ShoppingDataHelper(Context context) {
        super(context, TABLE_NAME, null, version);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(createTable);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME + ";");
        onCreate(db);
    }
}

重点分享过程中遇到的关于SQLITE数据库中字段大小写的问题,建议创建数据库的语句全部用小写,这样可以避免因大小写产生的不必要麻烦,如确实需求涉及到大小写,可以参考如下链接,有介绍几种关于Sqlite查询和处理过程中忽略大小写的方法

SQLite忽略大小写的方法




  

  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值