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
SQLiteOpenHelper+ContentProvider的使用的更多相关文章
- ContentResolver + SqliteOpenHelper + ContentProvider 理解
惭愧,现在才接触到ContentResolver的用法 这个类主要是Android用来实现应用程序之间数据共享的 一个应用程序可以将自己的数据完全暴露出去,外界更本看不到,也不用看到这个应用程序暴露的 ...
- Android之ContentProvider数据存储
一.ContentProvider保存数据介绍 一个程序可以通过实现一个ContentProvider的抽象接口将自己的数据完全暴露出去,而且ContentProvider是以类似数据库中表的方式将数 ...
- Xamarin.Android之ContentProvider
一.前言 掌握了如何使用SQLiteOpenHelper之后,我们就可以进行下一步的学习.本章我们将会学习如何使用ContentProvider来将数据库方面的操作封装起来,同时它还可以供其他应用访问 ...
- ContentProvider域名替换小工具
开发项目域名想怎么换就怎么换,就是这么任性! 这是一个很有意思的小工具! 这是一个方便开发人员和测试人员的小工具!! 吐槽: 一直在做Android开发,一直总有一个问题存在:做自己公司的apk开发时 ...
- 简单的学习心得:网易云课堂Android开发第六章SQLite与ContentProvider
一.SQLite 1.基本操作: (1)创建数据库:在SQLiteOpenHelper的子类构造器中创建. (2)创建表:在SQLiteOpenHelper的子类onCreate方法中,调用execS ...
- 安卓初級教程(3):ContentProvider的運用原理
package com.example.android.provider; import java.util.ArrayList; import java.util.HashMap; import j ...
- Android笔记——SQLiteOpenHelper类
public 抽象类 SQLiteOpenHelper 继承关系 Java.lang.Object android.database.sqlite.SQLiteOpenHelper 类概要 这是一个辅 ...
- Android细笔记--ContentProvider
Provider的不常见访问方式 Batch access:访问ContentProvider的一中模式,使用该模式可以同时对provider进行多个操作,且支持同时操作多个表.使用时首先构建一个Co ...
- Android四大组件--ContentProvider详解(转)
一.相关ContentProvider概念解析: 1.ContentProvider简介在Android官方指出的Android的数据存储方式总共有五种,分别是:Shared Preferences. ...
随机推荐
- 17、字符串转换整数 (atoi)
17.字符串转换整数 (atoi) 请你来实现一个 atoi 函数,使其能将字符串转换成整数. 首先,该函数会根据需要丢弃无用的开头空格字符,直到寻找到第一个非空格的字符为止. 当我们寻找到的第一个非 ...
- Flexbox弹性布局
Flexbox,一种CSS3的布局模式,也叫做弹性盒子模型,用来为盒装模型提供最大的灵活性.最新版本兼容IE11+.firefox.safari.chrome.opera及移动端,但移动端ios7.1 ...
- location.origin兼容IE
if (window["context"] == undefined) { if (!window.location.origin) { window.location.origi ...
- BZOJ5118: Fib数列2(二次剩余)
题意 题目链接 题目链接 一种做法是直接用欧拉降幂算出\(2^p \pmod{p - 1}\)然后矩阵快速幂. 但是今天学习了一下二次剩余,也可以用通项公式+二次剩余做. 就是我们猜想\(5\)在这个 ...
- 【Dojo 1.x】笔记目录
学习笔记和教程是不同的,笔记是随心记,学到什么就写什么,我尽量按逻辑顺序写笔记. Dojo是什么? Dojo是这么一个JavaScript框架,区别于jQuery等小型类库,这个类库更合适于构建Web ...
- 自定义HorizontalScrollView的scrollBar
尊重劳动成果,转载请标明出处http://www.cnblogs.com/tangZH/p/8423803.html android滑动组件的scrollBar,看了不是很顺眼,没办法,因为项目需求, ...
- 探究高级的Kotlin Coroutines知识
要说程序如何从简单走向复杂, 线程的引入必然功不可没, 当我们期望利用线程来提升程序效能的过程中, 处理线程的方式也发生了从原始时代向科技时代发生了一步一步的进化, 正如我们的Elisha大神所著文章 ...
- unity can't add the script behaviour CallbackExecutor. The script needs ...
记一次Unity3D小问题 当我打开VS2017编辑完C#脚本,要将脚本拖到一个3D组件上时,发生了以下错误 unity can't add the script behaviour Callback ...
- nodejs 使用 js 模块
nodejs 使用 js 模块 Intro 最近需要用 nodejs 做一个爬虫,Google 有一个 Puppeteer 的项目,可以用它来做爬虫,有关 Puppeteer 的介绍网上也有很多,在这 ...
- sql server中嵌套事务*
转自 https://www.cnblogs.com/guanjie20/archive/2013/02/17/2914488.html 我们在写事务时经常遇到的问题如下: 消息 266,级别 16, ...