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:
1 // Defines a new Uri object that receives the result of the insertion 2 Uri mNewUri; 3 4 ... 5 6 // Defines an object to contain the new values to insert 7 ContentValues mNewValues = new ContentValues(); 8 9 /* 10 * Sets the values of each column and inserts the word. The arguments to the "put" 11 * method are "column name" and "value" 12 */ 13 mNewValues.put(UserDictionary.Words.APP_ID, "example.user"); 14 mNewValues.put(UserDictionary.Words.LOCALE, "en_US"); 15 mNewValues.put(UserDictionary.Words.WORD, "insert"); 16 mNewValues.put(UserDictionary.Words.FREQUENCY, "100"); 17 18 mNewUri = getContentResolver().insert( 19 UserDictionary.Word.CONTENT_URI, // the user dictionary content URI 20 mNewValues // the values to insert 21 );
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:
1 // Defines an object to contain the updated values 2 ContentValues mUpdateValues = new ContentValues(); 3 4 // Defines selection criteria for the rows you want to update 5 String mSelectionClause = UserDictionary.Words.LOCALE + "LIKE ?"; 6 String[] mSelectionArgs = {"en_%"}; 7 8 // Defines a variable to contain the number of updated rows 9 int mRowsUpdated = 0; 10 11 ... 12 13 /* 14 * Sets the updated value and updates the selected words. 15 */ 16 mUpdateValues.putNull(UserDictionary.Words.LOCALE); 17 18 mRowsUpdated = getContentResolver().update( 19 UserDictionary.Words.CONTENT_URI, // the user dictionary content URI 20 mUpdateValues // the columns to update 21 mSelectionClause // the column to select on 22 mSelectionArgs // the value to compare to 23 );
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.
1 // Defines selection criteria for the rows you want to delete 2 String mSelectionClause = UserDictionary.Words.APP_ID + " LIKE ?"; 3 String[] mSelectionArgs = {"user"}; 4 5 // Defines a variable to contain the number of rows deleted 6 int mRowsDeleted = 0; 7 8 ... 9 10 // Deletes the words that match the selection criteria 11 mRowsDeleted = getContentResolver().delete( 12 UserDictionary.Words.CONTENT_URI, // the user dictionary content URI 13 mSelectionClause // the column to select on 14 mSelectionArgs // the value to compare to 15 );
You should also sanitize user input when you call ContentResolver.delete(). To learn more about this, read the section Protecting against malicious input.
注意完全用户输入可能带来很大危害。