Search code examples
androidsqliteandroid-sqlite

How update column in table with array


This is my code :-

public static final String[] strArray = {
    "347",
    "87",
    "666",
    "102",
    "430",
    "26",
    "488",
    "654",
    "1230",
    "597"
};


for(int i=0;i<strArray.length;i++){
values.put("id", strArray[i]);
DB.update("book", values, "_id between 1 and 10", null);
}

This results with message : Sorry Only The Last element In Arry. See below picture :

enter image description here

All I want to do is, to update the column id with strArray. I want to add all strArray in column id where _id between 1 to 10 Please, can any one help? Thanks in advance.


Solution

  • Your code updates all the rows in each iteration of the for loop because of the condition _id between 1 and 10, so the final result is the update of the last iteration.
    Instead update only 1 row each time:

    for(int i = 0; i < strArray.length; i++) {
        values.put("id", strArray[i]);
        DB.update("book", values, "_id = ?", new String[] {String.valueOf(i + 1)});
    }