Contentprovider: delete Return Value

Source: Internet
Author: User

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!







Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.