Android Rooms - Search in String

2019-07-12 18:39发布

问题:

In Android Rooms persistence library, how would I write the following SQL statement:
SELECT * FROM table WHERE field LIKE %:value%
As a @Query?
This syntax is invalid, and I can't find anything about it in the docs.

回答1:

You can just concat using SQLite string concatenation.

@Query("SELECT * FROM table WHERE field LIKE '%' || :value  || '%'")


回答2:

Annswer by @yigit works great for me:

@Query("SELECT * FROM stores " +
        "WHERE name LIKE '%' || :search  || '%' " +
        "OR description LIKE '%' || :search  || '%'")