简记利用ContentProvider执行删除操作

先贴一下API文档的解释

public abstract int delete (Uri uri, String selection, String[] selectionArgs)

Added in  API level 1

Implement this to handle requests to delete one or more rows. The implementation should apply the selection clause when performing deletion, allowing the operation to affect multiple rows in a directory. As a courtesy, call notifyChange() after deleting. This method can be called from multiple threads, as described in Processes and Threads.

The implementation is responsible for parsing out a row ID at the end of the URI, if a specific row is being deleted. That is, the client would pass in content://contacts/people/22and the implementation is responsible for parsing the record number (22) when creating a SQL statement.

Parameters
uri The full URI to query, including a row ID (if a specific record is requested).
selection An optional restriction to apply to rows when deleting.
Returns
  • The number of rows affected.
Throws

SQLException

我乍一看API的介绍,发现参数介绍中并没有包括对第三个参数 String[] selectionArgs的介绍,一开始以为是我看走眼,在概述中应该能找到相关解释,后来仔细翻译过来也没有,只确定它是selection的参数,后来回忆了一下SQLite的SQL语句,想到了execSQL(String sql, Object[] bindArgs)方法,想到了一个例子。

sqliteDatabase.execsql("insert into table(_id,_name,_num) values(?,?,?)",new String[]{id,name,num});

这句语句表示插入三个参数到表table中,以此类比,delete方法可能为

getContentResolver().delete(URI , "id=?,name=?,num=?",new String[]{id,name,num});

意思是什么就很明了了,适合判断多组数据同时满足的操作。


你可能感兴趣的:(Android,感想)