1

I am using room persistent library in my project. I am trying to use 'like' clause in query but I am not able to use. I have tried both compile time (Query) and run time query (Raw Query ). Neither it's giving any kind of exception nor it's giving any output.

I have tried following ways but none of them worked :-

  1. Compile time Query -

    @Query("select * from recipe where type_of_diet like '%vegetarian%' ")
    public abstract DataSource.Factory<Integer, RecipeListPojo> 
    getRecipeListVeg();
    
  2. RawQuery -

    @RawQuery(observedEntities = RecipeListPojo.class)
    public abstract DataSource.Factory<Integer,RecipeListPojo> 
    getAllRecipesList(SupportSQLiteQuery sqLiteQuery);
    
    String query="select * from recipe  where type_of_diet like '%vegetarian%' ";
    SimpleSQLiteQuery simpleSQLiteQuery = new SimpleSQLiteQuery(query);
    recipeDao.getAllRecipesList(simpleSQLiteQuery);
    

compileSdkVersion 27

Library used - implementation "android.arch.persistence.room:runtime:1.1.0-beta3" annotationProcessor "android.arch.persistence.room:compiler:1.1.0-beta3"

Kindly let me know how can I run these type of queries.

Anushka Khare
  • 363
  • 3
  • 11

3 Answers3

5

Room 1.1.1 + RxJava2 + Kotlin it is working fine:

@Query("SELECT * FROM user WHERE name LIKE '%Tom%' ")
fun getUsers(): Single<List<UserEntity>>

With parameter:

@Query("SELECT * FROM user WHERE name LIKE '%' || :name || '%' ")
fun getUsers(name: String): Single<List<UserEntity>>
norbDEV
  • 4,795
  • 2
  • 37
  • 28
  • This works. Can you tell me how can we pass List and check if any of them is present instead of searching 1 name. – Rishabh876 Feb 20 '19 at 08:50
0

Syntax is not correct for your query. As per documentation, query would be similar to

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

Here arguments and key written with LIKE : must match.


Read Android Room - Select query with LIKE, which in kotlin but may clear this logic.

Pankaj Kumar
  • 81,967
  • 29
  • 167
  • 186
  • What if i don't want to compare it with some arguments , and i want to use some static value, like as I did in my example – Anushka Khare May 03 '18 at 06:23
0

You have to append before the symbol "%" as a prefix and postfix of your string to perform a patterned search, for example:

String param = "%"+mMyString+"%";

Then declare your DAO method, for example:

@Query("SELECT * FROM track_table WHERE title LIKE :param" + " OR artist LIKE :param" + " OR album like :param")
List< Track > search(String param);
LeoColman
  • 6,950
  • 7
  • 34
  • 63