delete from content provider in JAVA

iReminiscence

Member
Joined
Nov 29, 2009
Messages
249
Reaction score
0
Hello,

Im able to delete all the data from the content provider but
Just want to ask that if i want to delete only one data, how do i go about doing this?

what should be my code to access it was
getContentResolver().delete(addresses, ???? , ???);

or

getContentResolver().delete(addresses.buildUpon()
.appendPath(String.valueOf(???)).build(), null, null);

??:s22::s22:

public int delete(Uri uri, String selection, String[] selectionArgs) {
int count = 0;

System.out.println();

switch (uriMatcher.match(uri)) {
case ADDRESSES:
count = db.delete(ADDRESSES_TABLE_NAME, selection, selectionArgs);
break;
case ADDRESS_ID:
String id = uri.getLastPathSegment();
count = db.delete(ADDRESSES_TABLE_NAME, _ID
+ " = "
+ id
+ (!TextUtils.isEmpty(selection) ? " AND (" + selection
+ ')' : ""), selectionArgs);
break;
default:
throw new IllegalArgumentException("Unknown URI " + uri);
}

getContext().getContentResolver().notifyChange(uri, null);
return count;
}
 

davidktw

Arch-Supremacy Member
Joined
Apr 15, 2010
Messages
13,550
Reaction score
1,302
Hello,

Im able to delete all the data from the content provider but
Just want to ask that if i want to delete only one data, how do i go about doing this?

what should be my code to access it was
getContentResolver().delete(addresses, ???? , ???);

or

getContentResolver().delete(addresses.buildUpon()
.appendPath(String.valueOf(???)).build(), null, null);

??:s22::s22:

Code:
    public int delete(Uri uri, String selection, String[] selectionArgs) {
        int count = 0;

        System.out.println();

        switch (uriMatcher.match(uri)) {
            case ADDRESSES:
                count = db.delete(ADDRESSES_TABLE_NAME, selection, selectionArgs);
                break;
            case ADDRESS_ID:
                String id = uri.getLastPathSegment();
                count = db.delete(ADDRESSES_TABLE_NAME, _ID
                        + " = "
                        + id
                        + (!TextUtils.isEmpty(selection) ? " AND (" + selection
                        + ')' : ""), selectionArgs);
                break;
            default:
                throw new IllegalArgumentException("Unknown URI " + uri);
        }

        getContext().getContentResolver().notifyChange(uri, null);
        return count;
    }

This is Android development, is it not ? Next time, please do yourself a favour. Provide context. Can't even see what is "db" at first.
Your "delete" method where clause is going to clause SQLinjection if ever your parameters are input from the network or from other possible sources. ALways use Parameters Binding but supplying inputs from the selection arguments in the where clause. eg: "ID = ? AND ..."

Give more specific inputs and outputs. This is your own code, not some standard code recognised by the rest of the world.
 
Important Forum Advisory Note
This forum is moderated by volunteer moderators who will react only to members' feedback on posts. Moderators are not employees or representatives of HWZ Forums. Forum members and moderators are responsible for their own posts. Please refer to our Community Guidelines and Standards and Terms and Conditions for more information.
Top