Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

272
Visualizações
Android Room - Select query with LIKE

I'm trying to make a query to search all objects whose names contain text:

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

Messages:

Error:no viable alternative at input 'SELECT * FROM hamster WHERE name LIKE %'
Error:There is a problem with the query: [SQLITE_ERROR] SQL error or missing database (near "%": syntax error)
Error:Unused parameter: arg0

Also I'm trying:

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

Messages:

Error:Unused parameter: arg0

How to fix this?

over 4 years ago · Santiago Trujillo
4 Respostas
Responde à pergunta

0

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);
over 4 years ago · Santiago Trujillo Relatório

0

Some Example of LIKE Keyword using 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>>
    
  2. End with Search_Query

    @Query("SELECT * FROM hamster WHERE name LIKE '%' || :search_query ")
    fun loadHamsters(search_query: String?): Flowable<List<Hamster>>
    
  3. Get all which have Search_Query

    @Query("SELECT * FROM hamster WHERE name LIKE '%' || :search_query || '%'")
    fun loadHamsters(search_query: String?): Flowable<List<Hamster>>
    
  4. 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>>
    
over 4 years ago · Santiago Trujillo Relatório

0

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>>
over 4 years ago · Santiago Trujillo Relatório

0

You can just concat using SQLite string concatenation.

@Query("SELECT * FROM hamster WHERE name LIKE '%' || :search || '%'")
fun loadHamsters(search: String?): Flowable<List<Hamster>>
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda