Second Like does not work SQLIte

0

Well, when doing a query:

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

Only returns results through the description, ie the second like is not tested, or does not work. It gives no error, it just does not return anything. If I take 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?

    
asked by anonymous 10.04.2017 / 17:02

2 answers

1

There was one more "name" item 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 get an item in the above array to replace the corresponding "?" Since you use 2 "?", you need an Array with 2 items.     

08.05.2017 / 20:51
0

Entaum @genSkywalker I know, the "OR" works fine with "LIKE" in sqlite.

You need to mount your query using colunatbX LIKE '%string%' OR colunatbZ LIKE '%string%' or something like this.
Dai @Marcelowq, to solve 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);

By noting that in the LIKE clause of sql, the character "%" followed by any other non-extended character represents a string of characters in the query, ie a wildcard, so if you use '% string%' will return anything containing the string string in any position in the text.

As you posted a little code, we did not know what you already have, but that would solve for you.

With like I tested here in sqlite and returned what I quiz, in both positions of my LIKE .

    
10.04.2017 / 17:32