How to delete rows in SQLite with multiple where args?

前端 未结 6 953
陌清茗
陌清茗 2021-02-01 04:59

I want to delete rows which satisfy any of multiple conditions.

For example, I pass a list of IDs, and I want to delete all rows with these IDs (IDs are uni

6条回答
  •  無奈伤痛
    2021-02-01 05:35

    Here's an example which builds the "?, ?, ?, ..." placeholder string with StringBuilder. If there are many parameters, plain string concatenation would create lots of garbage, StringBuilder helps with that.

    String[] ids = {"0", "1", "2", "3",...};
    
    StringBuilder placeholders = new StringBuilder();
    for (int i = 0; i < ids.length; i++) {
        if (i != 0)
            placeholders.append(", ");
    
        placeholders.append("?");
    }
    
    String where = "id IN (" + placeholders.toString() + ")";
    
    db.delete("rows", where, args);
    

提交回复
热议问题