Batch Delete items with Content Provider in Android

后端 未结 3 1765
长发绾君心
长发绾君心 2021-01-04 19:40

I\'m trying to batch delete some items in a table.

    String ids = { \"1\", \"2\", \"3\" };

    mContentResolver.delete(uri, MyTables._ID + \"=?\", ids);
<         


        
3条回答
  •  礼貌的吻别
    2021-01-04 20:24

    The error occurs because you have a single placeholder (?) in your where clause, while you pass three arguments. You should do:

    String ids = { "1", "2", "3" };
    
    mContentResolver.delete(uri, MyTables._ID + "=? OR " + MyTables._ID + "=? OR " + MyTables._ID + "=?", ids);
    

    I do not know if SQLite supports the IN clause, if so you could also do:

    String ids = { "1, 2, 3" };
    
    mContentResolver.delete(uri, MyTables._ID + " IN (?)", ids);
    

提交回复
热议问题