ContentProvider使用方法

本文详细介绍了ContentProvider的使用,包括ContentProvider的初始化、通过DBHelper获取数据库对象、重点讲解了query方法的使用,并提到了insert、delete和update等数据库操作方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

ContentProvider使用方法
  1. 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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值