According to Like does not work Sqlite

Asked

Viewed 197 times

0

Good, when doing a query:

        Cursor cursor = database.rawQuery("SELECT nome, imagem, descricao FROM Marcas WHERE descricao LIKE ? OR nome LIKE ?", new String[]{name});

It only returns results through the description, that is, the second like is not tested, or does not work. It does not give any error, it does not return anything. If I shoot the second like it works both by name and by description. But what I need is to search via name and description.

How to solve?

2 answers

1

So @genSkywalker that I know, "OR" works with "LIKE" on sqlite.

You need to assemble your query using colunatbX LIKE '%string%' OR colunatbZ LIKE '%string%' or something like.
Dai @Marcelowq, to solve in the best way, try to do something sign ó:

String ds = "'%"+tuaVarDesc+"%'";
String nm = "'%"+tuaVarNome+"%'";
String cmd = "SELECT nome, imagem, descricao FROM Marcas WHERE descricao LIKE "+ds+" OR nome LIKE "+nm;
Cursor cursor = database.rawQuery(cmd, null);

Lenbrando that in the clause LIKE sql, the character "%" followed by any other non-extended character, represents a string in the query, ie a wildcard, so if you use '%string%', it will return anything that contains the word string at any position in the text.

as Voce posted little code, we did not know what you already have there, but this would solve for Voce.

with like i tested here on sqlite and returned what I wanted, in both positions of my LIKE.

  • But what I want is to pass a string and search so much in nome how much in descricao. This string is a parameter when you call the method in the query.

  • but with that the parameter passed to "rawquery(...)" does just that, i.e., search by the word "string" at any position in the Description field OR in the name field.

  • So, what I’m saying. I want to do this but it’s not working. But the query is right, I run it on sqlite expert and it works. Only on android that not.

  • can post more code so I can have a clearer idea?

1

One more "name" item was missing in the array of the second parameter of the rawQuery method. It should look like this: new String[]{name, name}. For each "?" in the SQL string of the first parameter, the method tries to take an item in the cited array to replace the corresponding "?". How do you use 2 "?" , you need an Array with 2 items.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.