关于Android sqlite3数据库创建使用内容提供者contentProvider及解析者ContentResolver,观察者ContentResolver案例
来源:互联网 发布:ping不通阿里云服务器 编辑:程序博客网 时间:2024/06/11 01:58
数据库创建
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本地数据库的相关代码,同行攻城狮们有建议,请不吝赐教哦!
1 0
- 关于Android sqlite3数据库创建使用内容提供者contentProvider及解析者ContentResolver,观察者ContentResolver案例
- 内容提供者ContentProvider和内容解析者ContentResolver
- ContentProvider 和ContentResolver内容提供者和内容解析者
- ContentProvider内容提供者 和 ContentResolver内容解析者
- 内容提供者ContentProvider和内容解析者ContentResolver
- 内容提供者ContentProvider和内容解析者ContentResolver
- 内容提供者ContentProvider和内容解析者ContentResolver
- Android----ContentProvider(内容提供者) ContentResolver(内容访问者)
- android基础笔记:内容提供者及访问(ContentProvider、ContentResolver)
- Android知识点——内容提供者和内容观察者ContentProvider ContentResolver
- Android 中ContentProvider内容提供者和ContentResolver内容访问者
- Android:内容提供者(ContentProvider)和内容访问者(ContentResolver)
- Android ContentProvider(内容提供者 )+ContentResolver(内容访问者)
- Android之ContentProvider(内容的提供者)与ContentResolver(内容访问者)
- 内容提供者ContentProvider与内容接受者ContentResolver的使用详解
- 使用内容提供者ContentProvider与内容访问者ContentResolver实现拿到另外一个项目的数据库数据
- 内容观察者 contentResolver 使用
- ContentProvider内容提供者与ContentResolver内容访问者
- 前端性能优化
- spring中IOC和AOP详情注解
- web接入微博第三方登录
- linux minit Python2和python3切换
- SPOJ 1812 LCS2
- 关于Android sqlite3数据库创建使用内容提供者contentProvider及解析者ContentResolver,观察者ContentResolver案例
- 用java的反射实现将ResultSet自动转化成对象,自动产生sql语句
- MantisBT的安装配置及使用
- 深入理解计算机技术总结
- Android中的ContentProvider
- iOS 更加优雅便捷的UIAlertView/UIAlertController封装使用
- linux网络设置
- mount 错误 “RPC: failed to contact local rpcbind server (errno 5)
- 268. Missing Number 难度:medium