添加、更新和删除内容
2009-08-07 17:09
288 查看
在Content Provider上执行交互,通过调用ContentResolver对象的delete、update和insert方法。
Insert
ContentResolver提供了两个插入新的记录的方法——insert和bulkInsert。两个方法都接受你想添加的项目类型的URI;前者接受单一的ContentValues对象,后者接受一个数组。
简单的insert方法会返回新添加的记录的URI,而bulkInsert会返回成功添加的项目个数。
下面的代码片段显示了如何使用insert方法和bulkInsert方法:
// Create a new row of values to insert.
ContentValues newValues = new ContentValues();
// Assign values for each row.
newValues.put(COLUMN_NAME, newValue);
[ ... Repeat for each column ... ]
Uri myRowUri = getContentResolver().insert(MyProvider.CONTENT_URI, newValues);
// Create a new row of values to insert.
ContentValues[] valueArray = new ContentValues[5];
// TODO: Create an array of new rows
int count = getContentResolver().bulkInsert(MyProvider.CONTENT_URI, valueArray);
Delete
为了通过ContentResolver来删除一条记录,调用delete,传入你想删除的行的URI。可变通的地方,你可以指定一个where语句来删除多个行。两种技巧都在下面的片段中显示:
// Remove a specific row.
getContentResolver().delete(myRowUri, null, null);
// Remove the first five rows.
String where = “_id < 5”;
getContentResolver().delete(MyProvider.CONTENT_URI, where, null);
Update
通过在ContentResolver上调用update方法来对Content Provider进行更新。update方法需要目标Content Provider的URI,一个更新了数据的ContentValues对象,还有一个where语句来指定哪些行要被更新。
当执行时,与where语句匹配的行都会使用传入的ContentValues对象进行更新,并返回成功更新的行的数目。
// Create a new row of values to insert.
ContentValues newValues = new ContentValues();
// Create a replacement map, specifying which columns you want to
// update, and what values to assign to each of them.
newValues.put(COLUMN_NAME, newValue);
// Apply to the first 5 rows.
String where = “_id < 5”;
getContentResolver().update(MyProvider.CONTENT_URI, newValues, where, null);
Insert
ContentResolver提供了两个插入新的记录的方法——insert和bulkInsert。两个方法都接受你想添加的项目类型的URI;前者接受单一的ContentValues对象,后者接受一个数组。
简单的insert方法会返回新添加的记录的URI,而bulkInsert会返回成功添加的项目个数。
下面的代码片段显示了如何使用insert方法和bulkInsert方法:
// Create a new row of values to insert.
ContentValues newValues = new ContentValues();
// Assign values for each row.
newValues.put(COLUMN_NAME, newValue);
[ ... Repeat for each column ... ]
Uri myRowUri = getContentResolver().insert(MyProvider.CONTENT_URI, newValues);
// Create a new row of values to insert.
ContentValues[] valueArray = new ContentValues[5];
// TODO: Create an array of new rows
int count = getContentResolver().bulkInsert(MyProvider.CONTENT_URI, valueArray);
Delete
为了通过ContentResolver来删除一条记录,调用delete,传入你想删除的行的URI。可变通的地方,你可以指定一个where语句来删除多个行。两种技巧都在下面的片段中显示:
// Remove a specific row.
getContentResolver().delete(myRowUri, null, null);
// Remove the first five rows.
String where = “_id < 5”;
getContentResolver().delete(MyProvider.CONTENT_URI, where, null);
Update
通过在ContentResolver上调用update方法来对Content Provider进行更新。update方法需要目标Content Provider的URI,一个更新了数据的ContentValues对象,还有一个where语句来指定哪些行要被更新。
当执行时,与where语句匹配的行都会使用传入的ContentValues对象进行更新,并返回成功更新的行的数目。
// Create a new row of values to insert.
ContentValues newValues = new ContentValues();
// Create a replacement map, specifying which columns you want to
// update, and what values to assign to each of them.
newValues.put(COLUMN_NAME, newValue);
// Apply to the first 5 rows.
String where = “_id < 5”;
getContentResolver().update(MyProvider.CONTENT_URI, newValues, where, null);
相关文章推荐
- 简便的MS SQL 数据库 表内容 脚本 生成器,支持多级关联(含源代码)(更新:添加对NVARCHAR的特别支持、结果保存)
- Linux学习笔记-目录解释、添加删除用户、更新密码
- 步步为营VS 2008 + .NET 3.5(9) - DLINQ(LINQ to SQL)之执行SQL语句的添加、查询、更新和删除
- 步步为营VS 2008 + .NET 3.5(10) - DLINQ(LINQ to SQL)之调用存储过程的添加、查询、更新和删除
- Android 删除手机联系人,添加手机联系人,更新手机联系人信
- Eclipse中java向数据库中添加数据,更新数据,删除数据
- mysql 新增字段 添加字段 删除字段 修改字段 级联删除 级联更新 等
- LinQ To SQL 添加、删除、更新C#
- ASP.NET网页动态添加、更新或删除数据行
- 错误14274:无法添加、更新或删除从MSX服务器上发起的
- google 地图,添加文本标注,可提交到数据库(更新,删除,修改)
- 动态添加修改删除html表格内容
- 稳扎稳打Silverlight(57) - 4.0通信之WCF RIA Services: 概述, 通过 DomainDataSource 实现数据的添加、查询、更新和删除操作
- 稳扎稳打Silverlight(57) - 4.0通信之WCF RIA Services: 概述, 通过 DomainDataSource 实现数据的添加、查询、更新和删除操作
- 稳扎稳打Silverlight(57) - 4.0通信之WCF RIA Services: 概述, 通过 DomainDataSource 实现数据的添加、查询、更新和删除操作
- C#+AE要素的操作,添加、更新和删除要素
- 使用jQuery动态创建一个表格(根据用户输入的内容添加一行数据,并且能逐行删除)
- 9、步步为营VS 2008 + .NET 3.5(9) - DLINQ(LINQ to SQL)之执行SQL语句的添加、查询、更新和删除
- Window的内部机制(添加,删除,更新)
- 再接再厉VS 2008 sp1 + .NET 3.5 sp1(1) - Entity Framework(实体框架)之添加、查询、更新和删除的Demo