这里我有一个ID数组,并且只有在数组中有1个ID时,update方法才有效,但是如果我输入2-3,它会给我一个错误..类似:Too many bind arguments. 4 arguments were provided but the statement needs 2 arguments
这行得通。我想在同一列中为不同的ID设置相同的值。
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);
更新方法如下:
SQLiteDatabase db = mDbHelper.getWritableDatabase();
int rowsUpdated = db.update(DataEntry.TABLE_NAME, values, selection, selectionArgs);
答案 0 :(得分:0)
我相信您可以使用:-
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});
}
或者您可以使用WHERE IN (list of id's)
,它可能与:-
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()});
当然,您可以循环使用x个占位符。这将是:-
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);