9

テーブル内のいくつかのアイテムを一括削除しようとしています。

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

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

ただし、この次のエラーが引き続き発生します

java.lang.IllegalArgumentException: バインド引数が多すぎます。3 つの引数が指定されましたが、ステートメントには 1 つの引数が必要です。

4

3 に答える 3

21

ContentProviderOperationを使用すると、1 つのトランザクションで一括削除/挿入/更新を行うことができます。文字列を連結する必要がない方がはるかに優れています。また、非常に効率的である必要があります。削除の場合:

    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) {

    }
于 2014-04-02T21:36:55.163 に答える
10

エラーが発生するのは、where 句にプレースホルダー (?) が 1 つあるのに、3 つの引数を渡すためです。やったほうがいい:

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

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

SQLite が IN 句をサポートしているかどうかはわかりません。サポートしている場合は、次のこともできます。

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

mContentResolver.delete(uri, MyTables._ID + " IN (?)", ids);
于 2012-07-12T19:04:00.153 に答える