Search code examples
androidsqlandroid-roomandroid-architecture-components

How can I use SQL "NOT IN" Operator with Room


@Query("UPDATE items SET saved=:saved WHERE id IN :itemsIds") // works
abstract void method1(List<Long> itemsIds, boolean saved);

@Query("UPDATE items SET saved=:saved WHERE id NOT IN :itemsIds") // ERROR!!
abstract void method2(List<Long> itemsIds, boolean saved);

@Transaction
void updatePreferredItems(@NonNull List<Long> prefItems) {
    method1(prefItems, true);
    method2(prefItems, false);
}

My objective is from an ids list I would like to to update the field saved of all the items to true if item id belong to ids list, false otherwise.

Why the second query is generating a compile error ?

Is this the right approach ?


Solution

  • What happens when you do this:

    @Query("UPDATE items SET saved=:saved WHERE NOT(id IN :itemsIds)") // ERROR??
    abstract void method2(List<Long> itemsIds, boolean saved);