The delete operation of contentprovider can delete data, and this method returns the number of records of the currently deleted data.
Sdk api description
However, after reading the following content, you will find that this is not the case on the surface!
The content of the existing database table is as follows:
1. on the simulator of android2.3.3
Use the following statement to delete records with _ id = 1
final int delRows = getContentResolver().delete(UserTableData.CONTENT_URI, "_id=1", null);Log.d("mark", "delete data and delRows = " + delRows);
Return value information
If you delete all the data in the table in the database, you can
@Overridepublic void onClick(View v) {final int delRows = getContentResolver().delete(UserTableData.CONTENT_URI, null, null);Log.d("mark", "delete data and delRows = " + delRows);}
Return value information
The returned information is correct!
2. On the android2.1 real machine
Run the same program on the real machine, delete a record, and the returned results are:
However, if you delete all the data, the returned result is
Does not match the actual situation.
I thought that the deletion failed, but I opened the database to check whether there was no data in it ?!
It is estimated that the Android version is incorrect.
3. Solve the Problem
Using contentprovider to delete data is actually calling the delete method of sqlitedatabase.
Delete method of sqlitedatabase in Android SDK API
After careful research, we found that to remove all rows and get a count pass "1" as the whereclause.
So the code is modified.
final int delRows = getContentResolver().delete(UserTableData.CONTENT_URI, "1", null);Log.d("mark", "delete data and delRows = " + delRows);
Again, the test code, the real machine and the simulator both returned the correct results!
Solve the problem!