數據庫創建
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本地數據庫的相關代碼,同行攻城獅們有建議,請不吝賜教哦!