public class MyDatabaseProvider extends ContentProvider {
public static final int BOOK_DIR=0;
public static final int BOOK_ITEM=1;
public static final int H_DIR=2;
public static final int H_ITEM=3;
public static final String PATH="com.lingzhuo.testdatabase.provider";
private MyDatabaseHelper myDatabaseHelper;
private static UriMatcher uriMatcher;
static {
uriMatcher=new UriMatcher(UriMatcher.NO_MATCH);
uriMatcher.addURI(PATH,"book",BOOK_DIR);
uriMatcher.addURI(PATH,"book/#",BOOK_ITEM);
uriMatcher.addURI(PATH,"helloword",H_DIR);
uriMatcher.addURI(PATH,"helloword/#",H_ITEM);
}
@Override
public boolean onCreate() {
myDatabaseHelper=new MyDatabaseHelper(getContext(),"school.db",null,2);
return true;
}
@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
SQLiteDatabase db=myDatabaseHelper.getWritableDatabase();
Cursor cursor=null;
switch (uriMatcher.match(uri)){
case BOOK_DIR:
cursor=db.query("book",projection,selection,selectionArgs,null,null,sortOrder);
break;
case BOOK_ITEM:
String bookid= uri.getPathSegments().get(1);
cursor=db.query("book",projection,"id=?",new String[]{bookid},null,null,sortOrder);
break;
case H_DIR:
cursor=db.query("helloword",projection,selection,selectionArgs,null,null,sortOrder);
break;
case H_ITEM:
String helloid= uri.getPathSegments().get(1);
cursor=db.query("helloword",projection,"id=?",new String[]{helloid},null,null,sortOrder);
break;
}
return cursor;
}
@Override
public Uri insert(Uri uri, ContentValues values) {
SQLiteDatabase db=myDatabaseHelper.getWritableDatabase();
Uri uriReturn=null;
switch (uriMatcher.match(uri)){
case BOOK_DIR:
case BOOK_ITEM:
long newBookId=db.insert("book",null,values);
uriReturn=Uri.parse("content://"+PATH+"/book/"+newBookId);
break;
case H_DIR:
case H_ITEM:
long newHId=db.insert("helloword",null,values);
uriReturn=Uri.parse("content://"+PATH+"/helloword/"+newHId);
break;
}
return uriReturn;
}
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
SQLiteDatabase db=myDatabaseHelper.getWritableDatabase();
int deleteRows=0;
switch (uriMatcher.match(uri)){
case BOOK_DIR:
deleteRows=db.delete("book",selection,selectionArgs);
break;
case BOOK_ITEM:
String bookid=uri.getPathSegments().get(1);
deleteRows=db.delete("book","id=?",new String[]{bookid});
break;
case H_DIR:
deleteRows=db.delete("helloword",selection,selectionArgs);
break;
case H_ITEM:
String hid=uri.getPathSegments().get(1);
deleteRows=db.delete("helloword","id=?",new String[]{hid});
break;
}
return deleteRows;
}
@Override
public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
SQLiteDatabase db=myDatabaseHelper.getWritableDatabase();
int updateRows=0;
switch (uriMatcher.match(uri)){
case BOOK_DIR:
updateRows=db.update("book",values,selection,selectionArgs);
break;
case BOOK_ITEM:
String bookid=uri.getPathSegments().get(1);
updateRows=db.update("book",values,"id = ?",new String[]{bookid});
break;
case H_DIR:
updateRows=db.update("helloword",values,selection,selectionArgs);
break;
case H_ITEM:
String hid=uri.getPathSegments().get(1);
updateRows=db.update("helloword",values,"id=?",new String[]{hid});
break;
}
return updateRows;
}
@Override
public String getType(Uri uri) {
switch (uriMatcher.match(uri)){
case BOOK_DIR:
return "vnd.android.cursor.dir/vnd.com.lingzhuo.testdatabase.provider.book";
case BOOK_ITEM:
return "vnd.android.cursor.item/vnd.com.lingzhuo.testdatabase.provider.book";
case H_DIR:
return "vnd.android.cursor.dir/vnd.com.lingzhuo.testdatabase.provider.helloword";
case H_ITEM:
return "vnd.android.cursor.item/vnd.com.lingzhuo.testdatabase.provider.helloword";
}
return null;
}
}
String hid=uri.getPathSegments().get(1);
getType(),根据传入的内容URI来返回相应的MIME类型。
getType()的返回值要求:
(1)必须以vnd开头
(2)如果内容以URI路径结尾,则后接android.cursor.dir/,如果内容URI以id结尾,则后接android.cursor.item/
(3)最后接上vnd.包名./provider.表名
private static UriMatcher uriMatcher;
static {
uriMatcher=new UriMatcher(UriMatcher.NO_MATCH);
uriMatcher.addURI(PATH,"book",BOOK_DIR);
uriMatcher.addURI(PATH,"book/#",BOOK_ITEM);
uriMatcher.addURI(PATH,"helloword",H_DIR);
uriMatcher.addURI(PATH,"helloword/#",H_ITEM);
}
@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
switch (uriMatcher.match(uri)){
case case BOOK_DIR:
//查询book表中的所有数据
break;
case BOOK_ITEM:
//查询book表中的单条数据
break;
case H_DIR:
//查询helloword表中的所有数据
break;
case H_ITEM:
//查询helloword表中的单条数据
break;
}
return null;
}
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.lingzhuo.testprovider2.MainActivity">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="增"
android:id="@+id/button_add" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="删"
android:id="@+id/button_delete" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="改"
android:id="@+id/button_change" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="查"
android:id="@+id/button_search" />
LinearLayout>
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private Button button_add,button_delete,button_change,button_search;
private String newId;
private Uri uri;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
private void init() {
button_add= (Button) findViewById(R.id.button_add);
button_change= (Button) findViewById(R.id.button_change);
button_delete= (Button) findViewById(R.id.button_delete);
button_search= (Button) findViewById(R.id.button_search);
button_add.setOnClickListener(this);
button_search.setOnClickListener(this);
button_delete.setOnClickListener(this);
button_change.setOnClickListener(this);
uri=Uri.parse("content://com.lingzhuo.testdatabase.provider/book");
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.button_add:
ContentValues values=new ContentValues();
values.put("name","Jack");
values.put("age","10086");
values.put("clazz","968574");
Uri newUri=getContentResolver().insert(uri,values);
newId=newUri.getPathSegments().get(1);
Log.d("**************id",newId);
break;
case R.id.button_delete:
Uri uri_delete=Uri.parse("content://com.lingzhuo.testdatabase.provider/book/"+newId);
getContentResolver().delete(uri_delete,null,null);
break;
case R.id.button_change:
Uri uri_change=Uri.parse("content://com.lingzhuo.testdatabase.provider/book/"+newId);
ContentValues values2=new ContentValues();
values2.put("name","aaaaaaaaaaaa");
values2.put("age","86001");
values2.put("clazz","135426");
getContentResolver().update(uri_change,values2,null,null);
break;
case R.id.button_search:
Cursor cursor=getContentResolver().query(uri,null,null,null,null);
if (cursor!=null){
while(cursor.moveToNext()){
String name=cursor.getString(cursor.getColumnIndex("name"));
int age=cursor.getInt(cursor.getColumnIndex("age"));
String clazz=cursor.getString(cursor.getColumnIndex("clazz"));
Toast.makeText(MainActivity.this, name + age + clazz, Toast.LENGTH_SHORT).show();
Log.d("**************",name+age+clazz);
}
}
break;
}
}
}