Search code examples
androidsqlitesql-updatebulkupdate

Bulk Update Database


I've been looking on this site for a while but have not found the answer. I am trying to do a bulk update on data that I know is already in the table. I have one column that needs to be set when a certain condition comes back for the row ID. Here is the single method but I want to make this more efficient and do it as a bulk. Our database is not in a Provider so I just using a Helper class.

public void markUnavailable(int myId) {
    SQLiteDatabase db = this.getWritableDatabase();
    String sql = "UPDATE " + MYTABLE + " SET " + Col.IS_AVAILABLE + "= 0"+ " WHERE " + Col.MY_ID + "=" + myId;
    db.execSQL(sql);
    db.close();
}

I would like to pass in an array of myIds to do the bulk Update. I can't do a Insert or Replace statement because I don't have access to all the column data and don't want to pass this through due to too many codes changes.

public void markUnavailable(int[] myId) {
   // ????
   /// need some way to loop through and update in bulk
}

Solution

  • Try UPDATE tablename SET column=0 WHERE ID IN (...), where ... is a comma-delimited list of ID values.