1.dbopenhelper
public class MyDataBaseOpenHelper extends SQLiteOpenHelper{
public MyDataBaseOpenHelper(Context context) {
super(context, "dataforroaming.db", null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table roamingdata(_id integer primary key autoincrement,key varchar(20),value varchar(20))");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
public class MyDatabaseUtils {
public MyDataBaseOpenHelper myDbhelp;
String mDbName="roamingdata";
public MyDatabaseUtils(Context mContext) {
super();
this.myDbhelp=new MyDataBaseOpenHelper(mContext);
}
public void addRoamingIfNo(RoamingBean mBean){
List mList=selectAllBeans();
List allKey=new ArrayList();
for(RoamingBean baen:mList){
allKey.add(baen.getKey());
}
if(allKey.contains(mBean.getKey())){
Log.e("have","===" +
"");
}else {
addRoaming(mBean);
}
}
public long addRoaming(RoamingBean mBean){
List mList=selectAllBeans();
List allKey=new ArrayList();
for(RoamingBean baen:mList){
allKey.add(baen.getKey());
}
long line=0;
if(allKey.contains(mBean.getKey())){
}else {
SQLiteDatabase mSqlite=myDbhelp.getReadableDatabase();
ContentValues values=new ContentValues();
Log.e("===",mBean.getKey()+mBean.getValue());
values.put("key",mBean.getKey());
values.put("value",mBean.getValue());
line=mSqlite.insert(mDbName,null,values);
}
return line;
}
public long updateRoaming(RoamingBean mBean){
long line=0;
SQLiteDatabase mSqlite=myDbhelp.getReadableDatabase();
ContentValues values=new ContentValues();
values.put("key",mBean.getKey());
values.put("value",mBean.getValue());
line=mSqlite.update(mDbName,values,"key=?", new String[]{mBean.getKey()});
return line;
}
public List selectAllBeans(){
List mList=new ArrayList();
SQLiteDatabase mSqlite=myDbhelp.getReadableDatabase();
Cursor mCursor=mSqlite.query(mDbName,null,null,null,null,null,null);
while (mCursor.moveToNext()){
RoamingBean bean=new RoamingBean();
bean.setKey(mCursor.getString(mCursor.getColumnIndex("key")));
bean.setValue(mCursor.getString(mCursor.getColumnIndex("value")));
mList.add(bean);
}
return mList;
}
public void deletAllData(){
SQLiteDatabase mSqlite=myDbhelp.getReadableDatabase();
mSqlite.delete(mDbName,null,null);
}
}
3.使用
textview.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
MyDatabaseUtils databaseUtils=new MyDatabaseUtils(getApplicationContext());
List mList= databaseUtils.selectAllBeans();
StringBuffer sb=new StringBuffer();
for(RoamingBean bean:mList){
sb.append(bean.getKey()+"--"+bean.getValue()+";");
}
textview.setText(sb.toString());
Log.e("allData",sb.toString());
}
});
4.加入contentprovide
public class MyContentProvide extends ContentProvider{
private SQLiteDatabase db;
private MyDatabaseUtils dbUtils;
public static UriMatcher uriMatcher=new UriMatcher(UriMatcher.NO_MATCH);
//uriMatcher.="content://com.example.shibo.mytest_contentprovide.contentprovide";
static {
uriMatcher.addURI("content://com.example.shibo.mytest_contentprovide.contentprovide","roamingdata",1);
}
@Override
public boolean onCreate() {
Log.e("开启开启","");
dbUtils=new MyDatabaseUtils(getContext());
db=dbUtils.myDbhelp.getReadableDatabase();
return true;
}
@Nullable
@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
return null;
}
@Nullable
@Override
public String getType(Uri uri) {
return null;
}
@Nullable
@Override
public Uri insert(Uri uri, ContentValues values) {
if(uriMatcher.match(uri)==1){
db.insert("roamingdata",null,values);
}else {
throw new IllegalArgumentException("不匹配!!");
}
return null;
}
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
return 0;
}
@Override
public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
Log.e("机内到欧派打他","");
int i=db.update("roamingdata",values,selection,selectionArgs);
return i;
}
}
6.contentprovide使用
textview.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ContentResolver contentResolver=getContentResolver();
ContentValues values=new ContentValues();
values.put("key","a");
values.put("value","赵日天");
contentResolver.update(Uri.parse("content://com.example.shibo.mytest_contentprovide.contentprovide"),values,"key=?",new String[]{"a"});
}
});