Android Room - Select query with LIKE

You can just concat using SQLite string concatenation.

@Query("SELECT * FROM hamster WHERE name LIKE '%' || :search || '%'")
fun loadHamsters(search: String?): Flowable<List<Hamster>>

Room only supports named bind parameter :name to avoid any confusion between the method parameters and the query bind parameters.

Room will automatically bind the parameters of the method into the bind arguments. This is done by matching the name of the parameters to the name of the bind arguments.

 @Query("SELECT * FROM user WHERE user_name LIKE :name AND last_name LIKE :last")
 public abstract List<User> findUsersByNameAndLastName(String name, String last);

You should enclose the % characters in your input query - not in the query itself.

E.g. try this:

@Query("SELECT * FROM hamster WHERE name LIKE :arg0")
fun loadHamsters(search: String?): Flowable<List<Hamster>>

Then your String search value should look like:

search = "%fido%";
loadHamsters(search);

Furthermore, the binding parameter name should match the variable name, so rather than arg0 it should look like:

@Query("SELECT * FROM hamster WHERE name LIKE :search")
fun loadHamsters(search: String?): Flowable<List<Hamster>>

Some Examples of LIKE Keywords used in dao with Room Database.

  1. Start with Search_Query
@Query("SELECT * FROM hamster WHERE name LIKE :search_query || '%'")
    fun loadHamsters(search_query: String?): Flowable<List<Hamster>>
  1. End with Search_Query
@Query("SELECT * FROM hamster WHERE name LIKE '%' || :search_query ")
   fun loadHamsters(search_query: String?): Flowable<List<Hamster>>
  1. Get all that have Search_Query
@Query("SELECT * FROM hamster WHERE name LIKE '%' || :search_query || '%'")
   fun loadHamsters(search_query: String?): Flowable<List<Hamster>>
  1. Use NOT LIKE For except data which have Search_Query
@Query("SELECT * FROM hamster WHERE name NOT LIKE '%' || :search_query || '%'")
   fun loadHamsters(search_query: String?): Flowable<List<Hamster>>