Android content provider

Access data in other programs:

There are generally two uses of content providers:

1. Use the existing content provider to read and manipulate the data in the corresponding program.

2. Create your own content provider to provide external access to the data of our program.

Basic usage of ContentResolver

To access the data shared in the content provider: you must use the ContentResolver class.
An instance of this class can be obtained through the getContentResolver() method in Context.
ContentResolver provides a series of methods for CRUD operations on data.

public Uri insert(Uri uri, ContentValues values)
//该方法用于往ContentProvider添加数据。

public int delete(Uri uri, String selection, String[] selectionArgs)
//该方法用于从ContentProvider删除数据。

public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs)
//该方法用于更新ContentProvider中的数据。

public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder)
//该方法用于从ContentProvider中获取数据。

You may find that the addition, deletion, modification, and query methods in ContentResolver do not receive the table name parameter, but use a Uri parameter instead, which is called the content URI. The content URI establishes a unique identifier for the data in the content provider, and it mainly consists of two parts:

1: authority: used to distinguish different applications. Generally, in order to avoid conflicts, package names are used for naming.

2: path: used to distinguish between different tables in the same application, usually added to the back of authority.

3: Finally, we also need to add a protocol statement to the head of the string.
E.g:

content://com.example.app.provider/table1
content://com.example.app.provider/table2

After getting the content URI string, we also need to parse it into a Uri object before it can be passed in as a parameter. code show as below:

Uri uri = Uri.parse("content://com.example.app.provider/table1");

Just call the Uri.parse() method to parse the content URI string into a Uri object.

Query the data in the table1 table

Cursor cursor = getContentResolver().query(
	uri,//指定查询某个应用程序下的某一张表
	projection,//指定查询的列名
	selection,//指定where的约束条件
	selectionArgs,//为where中的占位符提供具体的值
	sortOrder);//指定查询结果的排序方式
if(cursor != null){
    
    
	while(cursor.moveToNext()){
    
    
		String column1 = cursor.getString(cursor.getColumnIndex("column1"));
		int column2 = cursor.getInt(cursor.getColumnIndex("column2"));
	}
	cursor.close();
}

Add a piece of data:

ContentValues values = new ContentValues();
values.put("column1","text");
values.put("column2","1");
getContentResolver().insert(uri,values);

As you can see, the data to be added is still assembled into ContentValues, and then the insert() method of ContentResolver is called, and Uri and ContentValues ​​are passed in as parameters.

Update a piece of data

ContentValues values= new ContentValues();
values.put("column1","");
getContentResolver().update(uri,values,"column1 = ? and column2 = ?",new String []{
    
    "text","1"});

Note that the above code uses the selection and selectionArgs parameters to constrain the data to be updated to prevent all rows from being affected.

Delete a piece of data

getContentResolver().delete(uri,"column2 = ?",new String[]{
    
    "1"});

Guess you like

Origin blog.csdn.net/i_nclude/article/details/77651216