SQLiteOpenHelper+ContentProvider的使用

效果图:

SQLiteOpenHelper+ContentProvider的使用

PetDbHelper

package com.example.admin.pets;

import android.content.Context;import android.database.sqlite.SQLiteDatabase;import android.database.sqlite.SQLiteOpenHelper;import com.example.admin.pets.PetContract.PetEntry;

public class PetDbHelper extends SQLiteOpenHelper {

    private static  final  String DATABASE_NAME="shelter.db";    private static  final  int DATABASE_VERSION=1;

    public PetDbHelper(Context context){        super(context, DATABASE_NAME, null, DATABASE_VERSION);    }

    @Override    public void onCreate(SQLiteDatabase db) {        String SQL_CREATE_PETS_TABLE="CREATE TABLE "+PetEntry.TABLE_NAME+"("+ PetEntry._ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "                + PetEntry.COLUMN__PET_NAME + " TEXT NOT NULL, "                + PetEntry.COLUMN__PET_BREED + " TEXT, "                + PetEntry.COLUMN__PET_GENDER + " INTEGER NOT NULL, "                + PetEntry.COLUMN__PET_WEIGHT + " INTEGER NOT NULL DEFAULT 0);";        db.execSQL(SQL_CREATE_PETS_TABLE);

}

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

    }}

PetProvider
package com.example.admin.pets;

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 android.util.Log;import com.example.admin.pets.PetContract.PetEntry;

public class PetProvider extends ContentProvider {

    public static final String LOG_TAG = PetProvider.class.getSimpleName();    private PetDbHelper petDbHelper;    private static final UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH);    private static final int PETS = 100;    private static final int PETS_ID = 101;

    static {        matcher.addURI(PetContract.CONTENT_AUTHORITY, PetContract.PATH_PETS, PETS);        matcher.addURI(PetContract.CONTENT_AUTHORITY, PetContract.PATH_PETS + "/#", PETS_ID); }

    @Override    public boolean onCreate() {        petDbHelper=new PetDbHelper(getContext());        return true;    }

    @Override    public Cursor query( Uri uri,  String[] projection, String selection,  String[] selectionArgs,String sortOrder) {        SQLiteDatabase sqLiteDatabase=petDbHelper.getReadableDatabase();        Cursor cursor;       final int match=matcher.match(uri);        switch (match){            case PETS:                cursor = sqLiteDatabase.query(PetContract.PetEntry.TABLE_NAME, projection, selection, selectionArgs, null, null,                        sortOrder);                break;            case PETS_ID:                selection = PetContract.PetEntry._ID + "=?";                selectionArgs = new String[]{String.valueOf(ContentUris.parseId(uri))};                cursor = sqLiteDatabase.query(PetContract.PetEntry.TABLE_NAME, projection, selection, selectionArgs, null, null,                        sortOrder);                break;            default:                throw new IllegalArgumentException("Cannot query unknown URL" + uri);

        }        cursor.setNotificationUri(getContext().getContentResolver(),uri);        return cursor;    }

    @Override    public Uri insert( Uri uri,  ContentValues values) {        final int match = matcher.match(uri);        switch (match) {            case PETS:                return insertPet(uri, values);            default:                throw new IllegalArgumentException("Insertion is not supported for " + uri);        }    }

    private Uri insertPet(Uri uri, ContentValues values) {        String name = values.getAsString(PetEntry.COLUMN__PET_NAME);

        if (name == null) {            throw new IllegalArgumentException("Pet requires a name"); }

        Integer gender = values.getAsInteger(PetEntry.COLUMN__PET_GENDER);        if (gender == null || !PetEntry.isValidGender(gender)) {            throw new IllegalArgumentException("Pet requires valid gender"); }

        Integer weight = values.getAsInteger(PetEntry.COLUMN__PET_WEIGHT);        if (weight != null && weight < 0) {            throw new IllegalArgumentException("Pet requires valid weight"); }

        SQLiteDatabase database = petDbHelper.getWritableDatabase();

        long id = database.insert(PetEntry.TABLE_NAME, null, values);        if (id == -1) {            Log.e(LOG_TAG, "Failed to insert row for " + uri);            return null;        }

        getContext().getContentResolver().notifyChange(uri,null);        return ContentUris.withAppendedId(uri, id);    }

    @Override    public int delete( Uri uri,  String selection,String[] selectionArgs) {        int rowsDeleted;

        SQLiteDatabase database = petDbHelper.getWritableDatabase();

        final int match = matcher.match(uri);        switch (match) {            case PETS:                rowsDeleted = database.delete(PetEntry.TABLE_NAME, selection, selectionArgs);                break;            case PETS_ID:                selection = PetEntry._ID + "=?";                selectionArgs = new String[]{String.valueOf(ContentUris.parseId(uri))};                rowsDeleted = database.delete(PetEntry.TABLE_NAME, selection, selectionArgs);                break;            default:                throw new IllegalArgumentException("Deletion is not supported for " + uri);        }

        if (rowsDeleted != 0) {            getContext().getContentResolver().notifyChange(uri, null);        }        return rowsDeleted;

    }

    @Override    public int update( Uri uri,  ContentValues values,  String selection,  String[] selectionArgs) {        final int match = matcher.match(uri);        switch (match) {            case PETS:                return updatePet(uri, values, selection, selectionArgs);            case PETS_ID:                selection = PetEntry._ID + "=?";                selectionArgs = new String[]{String.valueOf(ContentUris.parseId(uri))};                return updatePet(uri, values, selection, selectionArgs);            default:                throw new IllegalArgumentException("Update is not supported for " + uri);        }    }

    private int updatePet(Uri uri, ContentValues values, String selection, String[] selectionArgs) {

        if (values.containsKey(PetEntry.COLUMN__PET_NAME)) {            String name = values.getAsString(PetEntry.COLUMN__PET_NAME);            if (name == null) {                throw new IllegalArgumentException("Pet requires a name"); } }

        if (values.containsKey(PetEntry.COLUMN__PET_GENDER)) {            Integer gender = values.getAsInteger(PetEntry.COLUMN__PET_GENDER);            if (gender == null || !PetEntry.isValidGender(gender)) {                throw new IllegalArgumentException("Pet requires valid gender"); } }

        if (values.containsKey(PetEntry.COLUMN__PET_WEIGHT)) {            Integer weight = values.getAsInteger(PetEntry.COLUMN__PET_WEIGHT);            if (weight != null && weight < 0) {                throw new IllegalArgumentException("Pet requires valid weight"); } }

        if (values.size() == 0) {            return 0; }

        SQLiteDatabase database = petDbHelper.getWritableDatabase();        int rowsUpdated = database.update(PetEntry.TABLE_NAME, values, selection, selectionArgs);

        if (rowsUpdated != 0) {            getContext().getContentResolver().notifyChange(uri, null); }

        return rowsUpdated;    }

    @Override    public String getType( Uri uri) {        final int match = matcher.match(uri);        switch (match) {            case PETS:                return PetEntry.CONTENT_LIST_TYPE;            case PETS_ID:                return PetEntry.CONTENT_ITEM_TYPE;            default:                throw new IllegalStateException("Unknown URI " + uri + " with match " + match);        }

    }

}
github项目源码: https://github.com/NeoWu55/Android-Pets
上一篇:search result


下一篇:iOS GET、POST数据解析