Android 内容提供器---内容提供器基础(插入、更新和删除数据)

用跟从提供器中获取数据同样的方式,你也可以使用提供器客户端和提供器的ContentProvider对象之间的相互作用来修改数据。你调用一个带有参数的ContentResolver对象的方法,这个参数会传递给相应的ContentProvider对象的方法。提供器和提供器客户端自动处理安全和进程间通信。

插入数据

要把数据插入到提供器中,就要调用ContentResolver.insert()方法。这个方法把一个新行插入到提供器中,并且返回这行的内容的URI。以下代码片段显示了怎样把一行新的数据插入到用户字典提供器:

// 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, "100");

mNewUri = getContentResolver().insert(
UserDictionary.Word.CONTENT_URI, // the user dictionary content URI
mNewValues // the values to insert
);

把一行新的数据放到一个单独的ContentValues对象中,它跟一行游标的格式类似。这个对象中的这些列不需要有相同的数据类型,并且如果你不想指定一个值,你能够使用ContentValues.putNull()方法给这列设置为null。

这个代码片段没有添加_ID列,因为这列是自动维护的。提供器会给添加的每一行分配一个唯一的_ID值。提供器通常使用这个值来作为表的主键。

在newUri中使用以下格式返回的内容URI标识了这个新追加的行:

content://user_dictionary/words/<id_value>

<id_value>是这个新行的_ID的值。大多数提供器都能自动的识别内容URI的这种格式,然后在这个特定行上执行申请操作。

调用ContentUris.parseId()方法,能够从返回的Uri中获得_ID值。

更新数据

要更新一行数据,就要像插入数据那样使用带有更新值的ContentValues对象,并且要像查询数据那样指定选择条件。然后使用ContentResolver.update()客户端方法。你仅需要给ContentValues对象添加需要更新列的值,如果要清除一列的值,把这个值设置为null即可。

下列代码片段把local列中所有语言为“en”的行改变为null。返回值是更新的行数。

// 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 = 0;

...

/*
* 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
);

在调用ContentResolver.update()方法时你也要过滤用户的输入,防治恶意输入。

删除数据

删除行跟获取行数据类似,你要指定想要删除的行的条件,客户端方法会返回被删除的行数。以下代码片段删除了appid列中与“user”匹配的行,并返回了被删除的行数。

// 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 = 0;

...

// 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
);

在调用ContentResolver.delete()方法时,也应该过滤用户输入,以防止恶意输入。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值