如果自己的数据对外提供接口,可以让别人修改自己的数据库,就用到contentProvider
怎么样定义自己需要的数据库,具体可以参考如下代码:
package com.stmars.sqlite;
import org.apache.http.client.utils.URIUtils;
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;
//定义一个类继承ContentProvider,复写ContentProvider中的insert,query,update,delete方法
public class MyContentProvider extends ContentProvider {
//提供操作数据库的uri
public final static Uri MY_URI=Uri.parse("content://com.stamars.sqlite.sqlite");
public final static String TAG="MyContentProvider";
MySQLiteOpenHelper mySQLiteOpenHelper=null;
//对query用到的URI分下类,方便返回不同的查找结果
public static UriMatcher uriMatcher=new UriMatcher(UriMatcher.NO_MATCH);
static{
uriMatcher.addURI("com.stamars.sqlite.sqlite", "student", 1);
uriMatcher.addURI("com.stamars.sqlite.sqlite", "teacher", 2);
uriMatcher.addURI("com.stamars.sqlite.sqlite", "image", 3);
uriMatcher.addURI("com.stamars.sqlite.sqlite", "student/#", 4);
uriMatcher.addURI("com.stamars.sqlite.sqlite", "channel", 5);
}
@Override
public Cursor query(Uri arg0, String[] arg1, String arg2, String[] arg3,
String arg4) {
SQLiteDatabase sldb=mySQLiteOpenHelper.getWritableDatabase();
int n=uriMatcher.match(arg0);
Log.i(TAG,"query uri="+arg0+",n="+n);
Cursor cursor=null;
switch (n) {
case 0:
break;
case 1:
Log.i(TAG, "case 1,arg0.toString="+arg0.toString());
cursor=sldb.query("student", arg1,
arg2, arg3, null, null, null);
break;
case 2:
break;
case 3:
break;
case 4:
long id=ContentUris.parseId(arg0);
cursor=sldb.query("student", new String[]{"id","name"},
"id=?",new String[]{""+id} , null, null, null);
break;
case 5:
cursor=sldb.query("channel", arg1,
arg2, arg3, null, null, null);
default:
break;
}
return cursor;
}
@Override
public int delete(Uri arg0, String arg1, String[] arg2) {
// TODO Auto-generated method stub
return 0;
}
@Override
public String getType(Uri arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public Uri insert(Uri arg0, ContentValues arg1) {
// TODO Auto-generated method stub
SQLiteDatabase sdb=mySQLiteOpenHelper.getWritableDatabase();
long id=-1;
if (arg0.toString().equals("content://com.stamars.sqlite.sqlite/channel"))
id=sdb.insert("channel", null, arg1);
else if(arg0.toString().equals("content://com.stamars.sqlite.sqlite/student")){
id=sdb.insert("student", null, arg1);
}else {
}
return ContentUris.withAppendedId(arg0, id);
}
//provider在AndroidManifest.xml中注册后,应用程序只要启动就会调用此方法,可以在创建provider的时候创建我们的数据库
@Override
public boolean onCreate() {
Log.i(TAG,"onCreate");
mySQLiteOpenHelper=new MySQLiteOpenHelper(getContext(),
"datatest", null, 1, null);
return (mySQLiteOpenHelper==null)?false:true;
}
@Override
public int update(Uri arg0, ContentValues arg1, String arg2, String[] arg3) {
SQLiteDatabase sdb=mySQLiteOpenHelper.getWritableDatabase();
int id=sdb.update("student", arg1, arg2, arg3);
Log.i(TAG, "update data id="+id);
return id;
//return 0;
}
}
怎么样来访问我们的数据库并对我们的数据库做操作,测试代码如下:
ContentValues contentValues=new ContentValues();
contentValues.put("_id", 3);
contentValues.put("name", "wangwu");
getContentResolver().insert(Uri.parse("content://com.stamars.sqlite.sqlite/student"), contentValues);
只需要在插入和删除的时候传入对应URI就可以访问定义这个URI的数据库了。
怎么样让数据库的数据更新实时反映到UI上呢,这就要用到观察者,数据库数据变化后通知UI更新。