简体   繁体   中英

Q: How to update 1 column with same data for different IDs (android)

Here I have an array of IDs and the update method works only if its 1 ID in the array, but if I put 2-3 it gives me an error..something like: Too many bind arguments. 4 arguments were provided but the statement needs 2 arguments Too many bind arguments. 4 arguments were provided but the statement needs 2 arguments How can I make it work. I want to set the same value in the same column for different IDs.

    ContentValues values = new ContentValues();
    values.put(DataEntry.COLUMN_DATA, "1");
    String selection = DataEntry._ID + "=?";
    String[] selectionArgs = selectedData.toArray(new String[selectedData.size()]);
    // To be a bit more clear the above line looks like this:
    // String[] selectionArgs = { 1, 2, 3, 4, 5 }; String array of IDs which varies 

    int rowsAffected = getContentResolver().update(DataEntry.CONTENT_URI, values, selection, selectionArgs);

The update method is as follows:

    SQLiteDatabase db = mDbHelper.getWritableDatabase();
    int rowsUpdated = db.update(DataEntry.TABLE_NAME, values, selection, selectionArgs);

I believe that you could use :-

ContentValues values = new ContentValues();
values.put(DataEntry.COLUMN_DATA, "1");
String selection = DataEntry._ID + "=?";
String[] selectionArgs = selectedData.toArray(new String[selectedData.size()]);
for each(String s: selectionArgs) {
    int rowsAffected = getContentResolver().update(DataEntry.CONTENT_URI, values, selection, new String[]{s});
}

Alternately you could use WHERE IN (list of id's) which could be along the lines of :-

ContentValues values = new ContentValues();
values.put(DataEntry.COLUMN_DATA, "1");
String selection " IN (?)";
String[] selectionArgs = selectedData.toArray(new String[selectedData.size()]);
StringBuilder sb = new StringBuilder();
for (int i=0; i < selectionArgs.length(); i++) {
    sb.append(selectionArgs[i]);
    if (i < (selectionArgs.length - 1)) {
        sb.append(",");
    }
}        
int rowsAffected = getContentResolver().update(DataEntry.CONTENT_URI, values, selection, new String[]{sb.toString()});

Of course you could loop through applying x placeholders. This would be along the lines of :-

ContentValues values = new ContentValues();
values.put(DataEntry.COLUMN_DATA, "1");
String[] selectionArgs = selectedData.toArray(new String[selectedData.size()]);
StringBuilder sb = new StringBuilder("IN (");
for (int i=0; i < selectionArgs.length(); i++) {
    if (i < (selectionArgs.length() - 1) {
        sb.append("?,");
    } else {
        sb.append("?)");
    }
}        
int rowsAffected = getContentResolver().update(DataEntry.CONTENT_URI, values, sb.toString, selectionArgs);
  • Note this is in-principle code and has not been tested, so it may contain some errors.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM