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. docker进入容器方法

    更简单的,建议大家下载 .bashrc_docker,并将内容放到 .bashrc 中. $ wget -P ~ https://github.com/yeasy/docker_practice/ra ...

  2. Javascript中的json操作

    <!doctype html> <html> <head> <title>extjs-json</title> <script typ ...

  3. backend flow

    在PD之后,netlist中会多出很多DCAP元件(去耦电容,减少IR-Drop)或者filter cell(保证芯片均匀度要求) 还有一些antenna cell也就是一些diode用来泻流,防止天 ...

  4. 20道C#练习题(二)11——20题

    11.一个游戏,前20关是每一关自身的分数,1-30关每一关是10分,31-40关,每一关是20分,1-49关,每一关是30分,第50关是100分,输入你现在闯到的关卡数,求你现在拥有的分数.利用if ...

  5. Native OR WebApp ?

    前两天刚好和一帮产品同学聊过,特指一个APP里面的各页面应该怎么做,大家的总结如下,原理一样,关键是了解Native和Web各自的优劣势:1. 偏交互的Native,偏浏览的Web:交互指复杂操作,输 ...

  6. OpenStack 界面开发中的排序问题

    Contents [hide] 1 需求 2 调研 3 排序的办法 4 解决代码 需求 获取主机列表的时候,希望能够对主机列表能分组显示,比如网络,一组网络段希望在一起显示 调研 openstack的 ...

  7. Hadoop实战2:MapReduce编程-WordCount实例-streaming-python环境

    这是搭建hadoop环境后的第一个MapReduce程序: 基于hadoop streaming的python的脚本: 1 map.py文件,把文本的内容划分成单词: #!/usr/bin/pytho ...

  8. python 三元运算符

    print (1==2) and 12 or 4 b=12 if 1==2 else 4print(b)

  9. innodb内部的并发线程

    1. innodb_thread_concurrency innodb有一系列的计数器来统计和控制内部的工作线程.其中最重要的一个是innodb_thread_concurrency,和它相关的inn ...

  10. 使用PowerDesigner生成Access数据库

    PowerDesigner生成Access数据库 自从使用PD以来一直知道可以支持access但一直没有搞明白如何通过脚本来创建access数据表.在PD的tools里终于找到的答案,具体 文件都在C ...