ContentProvider官方教程(5)ContentResolver插入、更新、删除 示例
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插入、更新、删除 示例的更多相关文章
- ContentProvider官方教程(3)ContentResolver查询、遍历 示例
Retrieving Data from the Provider This section describes how to retrieve data from a provider, using ...
- ContentProvider官方教程(4)ContentResolver权限
Content Provider Permissions A provider's application can specify permissions that other application ...
- ContentProvider官方教程(9)定义一个provider完整示例:实现方法,定义权限等
Creating a Content Provider In this document Designing Data Storage Designing Content URIs Implement ...
- 我的MYSQL学习心得(八) 插入 更新 删除
我的MYSQL学习心得(八) 插入 更新 删除 我的MYSQL学习心得(一) 简单语法 我的MYSQL学习心得(二) 数据类型宽度 我的MYSQL学习心得(三) 查看字段长度 我的MYSQL学习心得( ...
- oracle插入,更新,删除数据
插入,更新,删除数据 oracle提供了功能丰富的数据库管理语句 包括有效的向数据库中插入数据的insert语句 更新数据的update语句 以及当数据不再使用时删除数据的delete语句 更改数据之 ...
- sqlserver 插入 更新 删除 语句中的 output子句
官方文档镇楼: https://docs.microsoft.com/zh-cn/previous-versions/sql/sql-server-2008/ms177564(v=sql.100) 从 ...
- ContentProvider官方教程(2)简介、Content URIs
In this document Overview Accessing a provider Content URIs Content Provider Basics A content provid ...
- MariaDB 插入&更新&删除数据(8)
MariaDB数据库管理系统是MySQL的一个分支,主要由开源社区在维护,采用GPL授权许可MariaDB的目的是完全兼容MySQL,包括API和命令行,MySQL由于现在闭源了,而能轻松成为MySQ ...
- ContentProvider官方教程(1)何时用content provider
Content Providers Content providers manage access to a structured set of data. They encapsulate the ...
随机推荐
- 怎么查看windows2003中隐藏用户
在命令模式下删除1.你在MS-dos下先输入net user 看有那些用户, 注意第一步看不出隐藏的用户 2.然后在输入net localgroup administrators 或者 net loc ...
- 关于header跳转之后的乱码问题
关于header跳转之后的乱码问题 http://www.360doc.com/content/11/0603/19/7052474_121495648.shtml 问题:不同网站的跳转出现乱码,不同 ...
- angular 自定义指令
Template-expanding directive: <div ng-controller="Controller"> <div my-customer&g ...
- EBS登陆链接网址
SELECT home_url FROM icx_parameters; SELECT profile_option_value FROM fnd_profile_option_values WHER ...
- 夺命雷公狗---node.js---3commonJs 与 nodeJs的简介
JavaScript是一个强大面向对象语言,它有很多快速高效的解释器.官方JavaScript标准定义的API是为了构建基于浏览器的应用程序.然而,并没有定于一个用于更广泛的应用程序的标准库. Com ...
- Trace Sys
ARM片上调试和跟踪解决方案(包括CoreSight体系结构,嵌入式跟踪宏单元(ETM),程序流程跟踪(PTM),ARM调试接口(ADI), 跟踪缓冲器(ETB),嵌入式交叉触发器(CTM)) Cor ...
- scan & ATPG
Testability用来表征一个manufactured design的quality. 将testability放在ASIC前端来做,成为DFT(Design For Test),用可控(cont ...
- PAT乙级 1009. 说反话 (20)
1009. 说反话 (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 给定一句英语,要求你编写程序,将句中 ...
- 嵌套错误Inline markup blocks (@<p>Content</p>) cannot be nested. Only one level of inline markup is allowed
例子: @{Html.Telerik().Splitter().Name("MainSplitter") .Orientation(SplitterOrientation.Vert ...
- 一个Convert、TryParse数据转换的问题
今天在进行数据转换的时候遇到一个问题,记录下,希望看到的童鞋有点用哦~ Convert.ToInt32(0.80155023553515) 结果为1 但是以下的做法,就不是想当然的结果咯~ int.T ...