ContentResolver官方API文档(四大方法:增删改查),个人翻译

来源:互联网 发布:淘宝手机十大黑店 编辑:程序博客网 时间:2024/05/21 07:56

我这人就有病,总是不信别人,好了,干脆自己翻译一下API文档,cao,大白话翻译 哈哈


前言:先从 insert、update、delete、query、这四个重要的接口开始吧

一、insert方法

访问权限:public,即类下、同一个包下、其他包下都可以访问这个方法

不可重写:final,有这个关键字,代表该方法不能重写

接受参数:一个Uri实例对象, 一个ContentValues实例对象

参数详解:

url:你要插入的那张表的uri,例如:content://com.wangyuanwai.test/table(这个table就是一张表的名字) 

values:初始化的值,key代表的表里面的字段名字,传入一个空的ContentValues将创建一条空记录

返回值:其实就是ContentProvider下的insert返回的Uri,返回你新创建记录的Uri,例子:content://com.wangyuanwai.test/table/1(sqlite,主键 id,默认会自增,且id从1开始)

public final Uri insert (Uri url, ContentValues values)

Added in API level 1

Inserts a row into a table at the given URL. If the content provider supports transactions the insertion will be atomic.

Parameters
urlThe URL of the table to insert into.valuesThe initial values for the newly inserted row. The key is the column name for the field. Passing an empty ContentValues will create an empty row.
Returns
  • the URL of the newly created row.

  • 二、update方法
  • 访问权限:public
  • 能否重写:final
  • 接受参数:Uri实例对象,ContentValues实例对象,String实例对象,String引用对象集合(数组)
  • 参数详解:
  • uri:要改变哪一条记录,传入它的uri
  • values: 这个还是要改变的字段,key就是字段名称,如果传入一个null值,将移除所有已存在的字段的值
  • where:过滤,要更新哪一条记录:例子:update table set name = 'fuck' where id = 6;(这里的id = 6,就是这里的where参数)
  • 返回值:有几条记录被改变了,int类型的值,其实就是ContentProvider的update的返回值
  • 抛出异常:如果uri或者values哪一个为null,该方法会抛出NullPoniterException实例对象
  • public final int update (Uri uri, ContentValues values, String where, String[] selectionArgs)

    Added in API level 1

    Update row(s) in a content URI. If the content provider supports transactions the update will be atomic.

    Parameters
    uriThe URI to modify.valuesThe new field values. The key is the column name for the field. A null value will remove an existing field value.whereA filter to apply to rows before updating, formatted as an SQL WHERE clause (excluding the WHERE itself).
    Returns
    • the number of rows updated.
    Throws
    NullPointerExceptionif uri or values are null
  • 三、delete方法
  • 访问权限:public
  • 可否重写:final
  • 接受参数:Uri实例对象,String实例对象,String数组
  • 参数详解:
  • url:传入要删除的那条记录的Uri,例如:content://com.wangyuanwai.test/table/1,即第一条记录的uri
  • where:过滤要删除的记录,即SQL中的where语句条件
  • 返回值:删除记录的个数,其实就是ContentProvider的delete的返回值,牛逼
  • public final int delete (Uri url, String where, String[] selectionArgs)

    Added in API level 1

    Deletes row(s) specified by a content URI. If the content provider supports transactions, the deletion will be atomic.

    Parameters
    urlThe URL of the row to delete.whereA filter to apply to rows before deleting, formatted as an SQL WHERE clause (excluding the WHERE itself).
    Returns
    • The number of rows deleted.
  • 四、query方法(其中一个,因为我看到还有一个重载的方法)
  • 访问权限:public
  • 可否重写:final
  • 接受参数:Uri实例对象,String数组,String实例对象,String数组,String实例对象
  • 参数详解:
  • uri:传入你想查询的表的uri,例如:content://com.wangyuanwai.test/table  | 即表名为:table的 uri,你也可以传入:content://com.wangyuanwai.test/table/1 | 即表名为:table的,id为1的一条记录的Uri
  • projection:指定你要查询哪些字段,封装在String数组里,例如可以传入 new String[]{"id", "title", "content"}。代表你要返回id、title、content,这三个字段,如果这个参数你传入null,就会返回所有的字段,但是这样没有效率
  • selection:过滤哪些记录被返回到游标里,相当于是SQL中的where语句,这里我就不多说where了
  • selectionArgs:这个参数有意思,其实是对selection这个参数的补充,例如:selection,你可以传入 "title=?",然后你再传一个new String
  • []{"just", "do", "it"},这样的话,"just" "do" "it"就会依次替换到 ?里面,用做String的时候,用的比较多
  • sortOrder:这个传入排序规则,sql语句order by XXX, 传入的 就是排序的方案 
  • 返回值:一个Cursor实例对象,其实就是ContentProvider的query方法的返回值,哈哈
  • public final Cursor query (Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder)

    Added in API level 1

    Query the given URI, returning a Cursor over the result set.

    For best performance, the caller should follow these guidelines:

    • Provide an explicit projection, to prevent reading data from storage that aren't going to be used.
    • Use question mark parameter markers such as 'phone=?' instead of explicit values in the selection parameter, so that queries that differ only by those values will be recognized as the same for caching purposes.

    Parameters
    uriThe URI, using the content:// scheme, for the content to retrieve.projectionA list of which columns to return. Passing null will return all columns, which is inefficient.selectionA filter declaring which rows to return, formatted as an SQL WHERE clause (excluding the WHERE itself). Passing null will return all rows for the given URI.selectionArgsYou may include ?s in selection, which will be replaced by the values from selectionArgs, in the order that they appear in the selection. The values will be bound as Strings.sortOrderHow to order the rows, formatted as an SQL ORDER BY clause (excluding the ORDER BY itself). Passing null will use the default sort order, which may be unordered.
    Returns
    • A Cursor object, which is positioned before the first entry, or null
    See Also
    • Cursor

0 0
原创粉丝点击