Batch Delete items with Content Provider in Android
Asked Answered
S

3

9

I'm trying to batch delete some items in a table.

    String ids = { "1", "2", "3" };

    mContentResolver.delete(uri, MyTables._ID + "=?", ids);

However I keep getting this following error

java.lang.IllegalArgumentException: Too many bind arguments. 3 arguments were provided but the statement needs 1 arguments.

Sustentation answered 12/7, 2012 at 18:55 Comment(0)
E
10

The error occurs because you have a single placeholder (?) in your where clause, while you pass three arguments. You should do:

String ids = { "1", "2", "3" };

mContentResolver.delete(uri, MyTables._ID + "=? OR " + MyTables._ID + "=? OR " + MyTables._ID + "=?", ids);

I do not know if SQLite supports the IN clause, if so you could also do:

String ids = { "1, 2, 3" };

mContentResolver.delete(uri, MyTables._ID + " IN (?)", ids);
Erena answered 12/7, 2012 at 19:4 Comment(1)
Thanks Jan. I guess I had a fundamental misunderstanding of how the selectionArgs worked. My ids can be variable in size. So your second solution looks more appealing. However, I'm afraid that doesn't seem to work either. I suppose I could just check my size and loop to construct that where clase, but that doesn't seem nice :(Sustentation
B
21

You can use ContentProviderOperation for batch deletion/insertion/update in one transaction. It's much nicer you don't have to concatenate strings. It also should be very efficient. For deletion:

    ArrayList<ContentProviderOperation> operations = new ArrayList<ContentProviderOperation>();
    ContentProviderOperation operation;

    for (Item item : items) {

        operation = ContentProviderOperation
                .newDelete(ItemsColumns.CONTENT_URI)
                .withSelection(ItemsColumns.UID + " = ?", new String[]{item.getUid()})
                .build();

        operations.add(operation);
    }

    try {
        contentResolver.applyBatch(Contract.AUTHORITY, operations);
    } catch (RemoteException e) {

    } catch (OperationApplicationException e) {

    }
Barium answered 2/4, 2014 at 21:36 Comment(3)
Correct, I have this query whether BulkInsert is faster that ApplyBatchStrychnic
for sms operation the Authority is "sms"Rhombic
for song operation, the AUTHORITY id 'MediaStore.AUTHORITY'Bryner
E
10

The error occurs because you have a single placeholder (?) in your where clause, while you pass three arguments. You should do:

String ids = { "1", "2", "3" };

mContentResolver.delete(uri, MyTables._ID + "=? OR " + MyTables._ID + "=? OR " + MyTables._ID + "=?", ids);

I do not know if SQLite supports the IN clause, if so you could also do:

String ids = { "1, 2, 3" };

mContentResolver.delete(uri, MyTables._ID + " IN (?)", ids);
Erena answered 12/7, 2012 at 19:4 Comment(1)
Thanks Jan. I guess I had a fundamental misunderstanding of how the selectionArgs worked. My ids can be variable in size. So your second solution looks more appealing. However, I'm afraid that doesn't seem to work either. I suppose I could just check my size and loop to construct that where clase, but that doesn't seem nice :(Sustentation
P
0
String sqlCommand = String.format("DELETE FROM %s WHERE %s IN (%s);", TABLE_NAME, KEY_ID, 1,2,3);

db.execSQL(sqlCommand);
Pharmaceutics answered 2/12, 2017 at 15:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.