Search code examples
androidkotlinanko

How to delete rows in SQLite with multiple by where args using Anko?


I hope to delete a row by _id, I hope to delete multiple rows by where args using Anko too.

I have read the article at https://github.com/Kotlin/anko/wiki/Anko-SQLite#updating-values, but I have no result, could you help me?


Solution

  • First off, using the update method is wrong. Deleting is not the same as updating. Updating in SQL means changing the value of one or more fields in a row. What you are looking for is the delete method

    dbHelper.delete(TABLE_NAME, whereClause="Your actual where statement here", args)
    

    The where statement follows a slightly different syntax than the regular where clauses in SQLite. This is an exception to the update method because it has a method for "regular" SQL through a builder. The delete method does not have this though, so you have to use the Anko syntax.

    Essentially you can use this as a where clause:

    "someRow = {someRowData} AND differentRow = {otherData}"
    

    and in the arguments:

    "someRowData" to "whatever value you want",
    "otherData" to 1234
    

    I'm not entirely sure whether or not the AND keyword will work, the syntax isn't fully documented. I assume it should because there's nothing else documented. The general SQLite syntax should be valid, and only the argument replacement (to avoid SQL injection I assume) is different