关于Android sqlite3数据库创建使用内容提供者contentProvider及解析者ContentResolver,观察者ContentResolver案例
2016-12-30 22:33
916 查看
数据库创建
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; // 用户idString productid1=productid; // 产品idString prodNum1=prodNum+""; // 数量String proPropertyidcolor1=proPropertyidcolor+""; // 属性颜色idString proPropertyidsize1=proPropertyidsize+""; // 属性尺寸idvalues.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; // 用户idString productid1=productid; // 产品idString proPropertyidcolor1=proPropertyidcolor+""; // 属性颜色idString proPropertyidsize1=proPropertyidsize+""; // 属性尺寸idString 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=user4000id; // 用户idString 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+""; // 用户idString newcolor1=newcolor+""; // 属性颜色idString newsize1=newsize+""; // 属性尺寸id/*--------------- add begin ---------------*/String userid1=userid; // 用户idString productid1=productid; // 产品idString proPropertyidcolor1=proPropertyidcolor+""; // 属性颜色idString 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; // 用户idString productid1=productid; // 产品idString proPropertyidcolor1=proPropertyidcolor+""; // 属性颜色idString proPropertyidsize1=proPropertyidsize+""; // 属性尺寸idString 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; // 用户idString 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()) {@Overridepublic void onChange(boolean selfChange) {Toast.makeText(getContext(),"数据有变化",Toast.LENGTH_SHORT).show();refreshUI();super.onChange(selfChange);}});
以上为之前项目中Android本地数据库的相关代码,同行攻城狮们有建议,请不吝赐教哦!
相关文章推荐
- 【黑马Android】(04)数据库的创建和sql语句增删改查/LinearLayout展示列表数据/ListView的使用和BaseAdater/内容提供者创建
- android内容提供者ContentProvider,UriMatcher和内容观察者ContentObserver常见使用
- 使用内容提供者ContentProvider与内容访问者ContentResolver实现拿到另外一个项目的数据库数据
- 【黑马Android】(04)数据库的创建和sql语句增删改查/LinearLayout展示列表数据/ListView的使用和BaseAdater/内容提供者创建
- 无废话Android之listview入门,自定义的数据适配器、采用layoutInflater打气筒创建一个view对象、常用数据适配器ArrayAdapter、SimpleAdapter、使用ContentProvider(内容提供者)共享数据、短信的备份、插入一条记录到系统短信应用(3)
- Android(java)学习笔记245:ContentProvider使用(银行数据库创建和增删改查的案例)
- Android学习---通过内容提供者(ContentProvider)操作另外一个应用私有数据库的内容
- Android中内容提供者ContentProvider实现数据库增删改查
- Android自助餐之内容提供者ContentProvider使用
- 内容提供者ContentProvider和内容解析者ContentResolver
- Android 使用ContentProvider(内容提供者)共享数据
- Android(java)学习笔记254:ContentProvider使用之内容观察者(观察发出去的短信)
- [Android Pro] 内容提供者ContentProvider的基本使用
- Android开发13——内容提供者ContentProvider的基本使用
- Android开发13——内容提供者ContentProvider的基本使用 推荐
- Android开发13――内容提供者ContentProvider的基本使用
- 【Android基础】内容提供者ContentProvider的使用详解
- Android -- ContentProvider 内容提供者,创建和调用
- [Android Pro] 内容提供者ContentProvider的基本使用
- Android开发之内容提供者——创建自己的ContentProvider(详解)