您的位置:首页 > 编程语言 > PHP开发

ContentProvider使用方法

2015-09-12 19:33 627 查看


ContentProvider使用方法

ContentProvider文档中介绍

//声明该ContentProvider的唯一标识--通常使用包名+数据库名--必须小写
public static final String AUTHORITY ="com.qianfeng.gp08_day25_contentprovider1.users";

//为该组件中可以被外界访问的数据库中的资源定义Code标识
public static final int CODE_USER = 1;
public static final int CODE_ORDER = 8;
//定义访问资源的Uri的匹配器对象--使用该类生成被访问的资源的Uri
private static UriMatcher uriMatcher;


3.需要静态代码块初始化的
static{
//Creates the root node of the URI tree.获取根节点
uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
//content://com.qianfeng.gp08_day25_contentprovider1.users/user
uriMatcher.addURI(AUTHORITY, "user", CODE_USER);

//content://com.qianfeng.gp08_day25_contentprovider1.users/order
uriMatcher.addURI(AUTHORITY, "order", CODE_ORDER);
}


4.使用DeHelper获取数据库对像
@Override
public boolean onCreate() {
// TODO 初始化 数据库操作的工具类
dbHelper = new DBHelper(getContext());
return false;
}


6.重写query方法
@Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
SQLiteDatabase  db = dbHelper.getReadableDatabase();
Cursor cursor = null;

int code = uriMatcher.match(uri);

switch(code)
{
case CODE_USER:
cursor = db.query("t_user", projection, selection, selectionArgs, null, null, sortOrder);
break;
case CODE_ORDER:
break;
}
return cursor;
}


-上面的方法
Open Declaration int android.content.UriMatcher.match(Uri uri)

Try to match against the path in a url.

Parameters:
uri The url whose path we will match against.
Returns:
The code for the matched node (added using addURI), or -1 if there is no matched node.


#db.query(…)使用方法

Implement this to handle requests to insert a new row. As a courtesy, call notifyChange() after inserting. This method can be called from multiple threads, as described in Processes and Threads.

Overrides: insert(...) in ContentProvider
Parameters:
uri The content:// URI of the insertion request.
values A set of column_name/value pairs to add to the database.
Returns:
The URI for the newly inserted item.


db.insert(…)方法的使用

SQLiteDatabase db =dbHelper.getWritableDatabase();
db.insert(...)

Open Declaration long android.database.sqlite.SQLiteDatabase.insert(String table, String nullColumnHack, ContentValues values)

Convenience method for inserting a row into the database.

Parameters:
table the table to insert the row into
nullColumnHack optional; may be null. SQL doesn't allow inserting a completely empty row without naming at least one column name. If your provided values is empty, no column names are known and an empty row can't be inserted. If not set to null, the nullColumnHack parameter provides the name of nullable column name to explicitly insert a NULL into in the case where your values is empty.
values this map contains the initial column values for the row. The keys should be the column names and the values the column values
Returns:
the row ID of the newly inserted row, or -1 if an error occurred


上面方法的返回值

Open Declaration int com.qianfeng.gp08_day25_contentprovider1.contentprovider.UserContentProvider.delete(Uri uri, String selection, String[] selectionArgs)

@Override

Implement this to handle requests to delete one or more rows. The implementation should apply the selection clause when performing deletion, allowing the operation to affect multiple rows in a directory. As a courtesy, call notifyDelete() after deleting. This method can be called from multiple threads, as described in Processes and Threads.

The implementation is responsible for parsing out a row ID at the end of the URI, if a specific row is being deleted. That is, the client would pass in content://contacts/people/22 and the implementation is responsible for parsing the record number (22) when creating a SQL statement.

Overrides: delete(...) in ContentProvider
Parameters:
uri The full URI to query, including a row ID (if a specific record is requested).
selection An optional restriction to apply to rows when deleting.
selectionArgs
Returns:
The number of rows affected.


db.delete(…)方法的使用

`Open Declaration int com.qianfeng.gp08_day25_contentprovider1.contentprovider.UserContentProvider.update(Uri uri, ContentValues values, String selection, String[] selectionArgs)

@Override

Implement this to handle requests to update one or more rows. The implementation should update all rows matching the selection to set the columns according to the provided values map. As a courtesy, call notifyChange() after updating. This method can be called from multiple threads, as described in Processes and Threads.

Overrides: update(...) in ContentProvider
Parameters:
uri The URI to query. This can potentially have a record ID if this is an update request for a specific record.
values A Bundle mapping from column names to new column values (NULL is a valid value).
selection An optional filter to match rows to update.
selectionArgs
Returns:
the number of rows affected.``


db.update方法介绍

Open Declaration int android.database.sqlite.SQLiteDatabase.update(String table, ContentValues values, String whereClause, String[] whereArgs)

Convenience method for updating rows in the database.

Parameters:
table the table to update in
values a map from column names to new column values. null is a valid value that will be translated to NULL.
whereClause the optional WHERE clause to apply when updating. Passing null will update all rows.
whereArgs
Returns:
the number of rows affected
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: