テーブル内のいくつかのアイテムを一括削除しようとしています。
String ids = { "1", "2", "3" };
mContentResolver.delete(uri, MyTables._ID + "=?", ids);
ただし、この次のエラーが引き続き発生します
java.lang.IllegalArgumentException: バインド引数が多すぎます。3 つの引数が指定されましたが、ステートメントには 1 つの引数が必要です。
テーブル内のいくつかのアイテムを一括削除しようとしています。
String ids = { "1", "2", "3" };
mContentResolver.delete(uri, MyTables._ID + "=?", ids);
ただし、この次のエラーが引き続き発生します
java.lang.IllegalArgumentException: バインド引数が多すぎます。3 つの引数が指定されましたが、ステートメントには 1 つの引数が必要です。
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) {
}
エラーが発生するのは、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);