23

Having a Android Room with three tables timestamp, index, details, and all three have

@PrimaryKey @ColumnInfo(name = "id") var id: Int = 0

having fun clearDataByID(idList: List<Int>) to clear data from all three tables by the id in the idList

Dao as:

@Dao
interface DataDAO {


@Transaction
fun clearDataByID(idList: List<Int>) {
    deleteDataInTimestamp(idList)
    deleteDataIndex(idList)
    deleteDataDetails(idList)
}

@Query("delete from timestamp where id in :idList")
fun deleteDataInTimestamp(idList: List<Int>)

@Query("delete from index where id in :idList")
fun deleteDataIndex(idList: List<Int>)

@Query("delete from details where id in :idList")
fun deleteDataDetails(idList: List<Int>)
}

but it gets compiler error (similar for all three)

error: no viable alternative at input 'delete from timestamp where id in :idList'
public abstract void deleteDataInTimestamp(@org.jetbrains.annotations.NotNull()

if delete by single id it worked.

How to delete by a list of ids?

@Query("delete from timestamp where id = :id")
fun deleteSingleTimestamp(id: Int)
lannyf
  • 9,865
  • 12
  • 70
  • 152
  • Have you looked at [this answer](https://stackoverflow.com/a/48370198/10805404)? It explains how to do a `SELECT` with a list of ids. – Simon Doppler Apr 02 '19 at 15:40
  • thanks @SimonDoppler for point it out! it is the `obj_id IN (:ids)` missing the bracket. – lannyf Apr 02 '19 at 17:03

2 Answers2

55

Thanks Simon points to the similar question, it should be done like:

@Query("delete from timestamp where id in (:idList)")
fun deleteDataInTimestamp(idList: List<Int>) 
denvercoder9
  • 2,979
  • 3
  • 28
  • 41
lannyf
  • 9,865
  • 12
  • 70
  • 152
2

just extending the answer given by @lannyf

@Query("delete from timestamp where id in (:idList)")
fun deleteDataInTimestamp(idList: List<Int>) 

this could be written in this way also

@Query("delete from timestamp where id = :idList")
fun deleteDataInTimestamp(idList: List<Int>) 

PS: edit queue was full

  • 1
    The first way works! However the second one doesn't work The error is `near ",": syntax error (code 1 SQLITE_ERROR[1]): , while compiling: DELETE FROM product WHERE id = ?,?` – radus14 Nov 23 '22 at 13:06