Inserting, Updating, and Deleting Data

  In the same way that you retrieve data from a provider, you also use the interaction between a provider client and the provider's ContentProvider to modify data. You call a method of ContentResolver with arguments that are passed to the corresponding method of ContentProvider. The provider and provider client automatically handle security and inter-process communication. 

1.Inserting data 插入数据

  To insert data into a provider, you call the ContentResolver.insert() method. This method inserts a new row into the provider and returns a content URI for that row. This snippet shows how to insert a new word into the User Dictionary Provider:

 // Defines a new Uri object that receives the result of the insertion
Uri mNewUri; ... // Defines an object to contain the new values to insert
ContentValues mNewValues = new ContentValues(); /*
* Sets the values of each column and inserts the word. The arguments to the "put"
* method are "column name" and "value"
*/
mNewValues.put(UserDictionary.Words.APP_ID, "example.user");
mNewValues.put(UserDictionary.Words.LOCALE, "en_US");
mNewValues.put(UserDictionary.Words.WORD, "insert");
mNewValues.put(UserDictionary.Words.FREQUENCY, ""); mNewUri = getContentResolver().insert(
UserDictionary.Word.CONTENT_URI, // the user dictionary content URI
mNewValues // the values to insert
);

  The data for the new row goes into a single ContentValues object, which is similar in form to a one-row cursor. The columns in this object don't need to have the same data type, and if you don't want to specify a value at all, you can set a column to null using ContentValues.putNull().

  ContentValues.putNull() 可插入空值。

  The snippet doesn't add the _ID column, because this column is maintained automatically. The provider assigns a unique value of _ID to every row that is added. Providers usually use this value as the table's primary key.

  The content URI returned in newUri identifies the newly-added row, with the following format:

  插入返回的结果是个URI.格式如下:
  content://user_dictionary/words/<id_value>

  The <id_value> is the contents of _ID for the new row. Most providers can detect this form of content URI automatically and then perform the requested operation on that particular row.

  To get the value of _ID from the returned Uri, call ContentUris.parseId().

  这个_ID可以通过ContentUris.parseId()解析值。

2.Updating data 更新数据

  To update a row, you use a ContentValues object with the updated values just as you do with an insertion, and selection criteria just as you do with a query. The client method you use is ContentResolver.update(). You only need to add values to the ContentValues object for columns you're updating. If you want to clear the contents of a column, set the value to null.

  ContentResolver.update() 更新字段,传入具体字段值就可,传空值是置空。返回值是更新的数目。

  The following snippet changes all the rows whose locale has the language "en" to a have a locale of null. The return value is the number of rows that were updated:

 // Defines an object to contain the updated values
ContentValues mUpdateValues = new ContentValues(); // Defines selection criteria for the rows you want to update
String mSelectionClause = UserDictionary.Words.LOCALE + "LIKE ?";
String[] mSelectionArgs = {"en_%"}; // Defines a variable to contain the number of updated rows
int mRowsUpdated = ; ... /*
* Sets the updated value and updates the selected words.
*/
mUpdateValues.putNull(UserDictionary.Words.LOCALE); mRowsUpdated = getContentResolver().update(
UserDictionary.Words.CONTENT_URI, // the user dictionary content URI
mUpdateValues // the columns to update
mSelectionClause // the column to select on
mSelectionArgs // the value to compare to
);

  You should also sanitize user input when you call ContentResolver.update(). To learn more about this, read the section Protecting against malicious input.

  注意完全用户输入可能带来很大危害。

3.Deleting data 删除数据

  Deleting rows is similar to retrieving row data: you specify selection criteria for the rows you want to delete and the client method returns the number of deleted rows. The following snippet deletes rows whose appid matches "user". The method returns the number of deleted rows.

 // Defines selection criteria for the rows you want to delete
String mSelectionClause = UserDictionary.Words.APP_ID + " LIKE ?";
String[] mSelectionArgs = {"user"}; // Defines a variable to contain the number of rows deleted
int mRowsDeleted = ; ... // Deletes the words that match the selection criteria
mRowsDeleted = getContentResolver().delete(
UserDictionary.Words.CONTENT_URI, // the user dictionary content URI
mSelectionClause // the column to select on
mSelectionArgs // the value to compare to
);

  You should also sanitize user input when you call ContentResolver.delete(). To learn more about this, read the section Protecting against malicious input.

  注意完全用户输入可能带来很大危害。

ContentProvider官方教程(5)ContentResolver插入、更新、删除 示例的更多相关文章

  1. ContentProvider官方教程(3)ContentResolver查询、遍历 示例

    Retrieving Data from the Provider This section describes how to retrieve data from a provider, using ...

  2. ContentProvider官方教程(4)ContentResolver权限

    Content Provider Permissions A provider's application can specify permissions that other application ...

  3. ContentProvider官方教程(9)定义一个provider完整示例:实现方法,定义权限等

    Creating a Content Provider In this document Designing Data Storage Designing Content URIs Implement ...

  4. 我的MYSQL学习心得(八) 插入 更新 删除

    我的MYSQL学习心得(八) 插入 更新 删除 我的MYSQL学习心得(一) 简单语法 我的MYSQL学习心得(二) 数据类型宽度 我的MYSQL学习心得(三) 查看字段长度 我的MYSQL学习心得( ...

  5. oracle插入,更新,删除数据

    插入,更新,删除数据 oracle提供了功能丰富的数据库管理语句 包括有效的向数据库中插入数据的insert语句 更新数据的update语句 以及当数据不再使用时删除数据的delete语句 更改数据之 ...

  6. sqlserver 插入 更新 删除 语句中的 output子句

    官方文档镇楼: https://docs.microsoft.com/zh-cn/previous-versions/sql/sql-server-2008/ms177564(v=sql.100) 从 ...

  7. ContentProvider官方教程(2)简介、Content URIs

    In this document Overview Accessing a provider Content URIs Content Provider Basics A content provid ...

  8. MariaDB 插入&更新&删除数据(8)

    MariaDB数据库管理系统是MySQL的一个分支,主要由开源社区在维护,采用GPL授权许可MariaDB的目的是完全兼容MySQL,包括API和命令行,MySQL由于现在闭源了,而能轻松成为MySQ ...

  9. ContentProvider官方教程(1)何时用content provider

    Content Providers Content providers manage access to a structured set of data. They encapsulate the ...

随机推荐

  1. linux第5天 socket api

    IPv4套接口地址结构通常也称为“网际套接字地址结构”,它以“sockaddr_in”命名,定义在头文件<netinet/in.h>中 通用地址结构用来指定与套接字关联的地址.以socka ...

  2. MVC3/4 自定义HtmlHelper截断文本内容(截取)

    在MVC目录下新建一个名为 Extersions  的文件夹,在该文件夹中新建一个截断文本类,取名为:CutOfTextExtersions 该类代码如下: using System; using S ...

  3. SQL语句:find_in_set的使用方法

    如果我们有一张表: 里面有的信息如下: 我们需要查找出friends字段里面包含11的值. 我们传统的方法是: %"; 但是这样查到的结果2条的,不大符合我们的需求,如下所示: 我们只想查找 ...

  4. QTP11.00安装+破解详细教程

    一.      安装过程 首先双击setup.exe文件,选择“QuickTest Professional安装程序” 此时会查看你机子上面是否有QTP需要,但是机子上没有的组件, 跟着先安装这两个组 ...

  5. Android判断网络状态

    package com.ch.services; import com.ch.utils.NetWorkUtils; import android.app.Service; import androi ...

  6. 161107、spring异步调用,完美解决!

    项目中,用户抢单,下单需要向对方推送消息,但是加上推送就会造成抢单和下单性能降低,反应变慢,因为抢单下单动作跟推送部分是同步的,现在想改成异步推送. 在Java应用中,绝大多数情况下都是通过同步的方式 ...

  7. 如何自动生成Facade 的EJB

    1.jbuilder中连接数据库,注意:java:/DataSource 2.选择数据表,右健选择"create cmp 2.x..." 3.添加"findAll&quo ...

  8. postgres创建用户,表

    使用createuser来创建用户 [postgres@web1 ~]$ /data/pgsql/bin/createuser --help createuser creates a new Post ...

  9. listview 滑动以后设置最上面一行为整行展示

    需求: listview显示的第一行永远为整行,不能为半行. 参考: android listview 每次滑动整行 1. 添加 listview 的 setOnScrollListener() 事件 ...

  10. MySQL start and stop

    一.本文说明 本实验主要是演示MySQL的四种启动方式,附带停止的操作. 二.mysqld mysqld is the MySQL server   mysqld reads options from ...