How do I search for a string in a table on Android?

0

I want to search using the "clas" field, which is a String. And listSearch receives a String.

    public Cursor listPesquisar(String pesquisa) {
        Cursor cursor;
        String[] fields = new String[]{"_id", "nome", "peso", "altura", "cep", "telefone", "idade", "resultado", "clas"};

        String where = "clas =" + pesquisa;
        db = banco.getReadableDatabase();
        cursor = db.query(DadosDB.NOME_TABELA, fields, where,null, null, null, null, null);

        if (cursor != null) {
            cursor.moveToFirst();
        }
        db.close();
        return cursor;
    }
    
asked by anonymous 13.06.2017 / 21:16

2 answers

3

If you search the column nome , you can do this using the = :

cursor = db.query(DadosDB.NOME_TABELA, fields, "nome =?",
            new String[] {"nome"}, null, null, null, null);

But this case is if you look exactly at the value of string pesquisa . You can also use LIKE . See this question about difference between the ' = 'and LIKE .

As you are doing a search in the clas column, you can do this:

String where = "clas LIKE '%" + pesquisa + "%'";
cursor = db.query(DadosDB.NOME_TABELA, fields, where, null, null, null, null, null);
  

[...] LIKE looks for something like ", ie content that has the text   searched for in a part of where (column (s)) you are looking for. In   general the% symbol is used to indicate where it can have characters   joker, where you can have any other thing. Bigown

    
13.06.2017 / 21:38
1

Modify the line that uses the "search" parameter like this:

String where = "clas like '%" + pesquisa + "%'"; 
    
13.06.2017 / 21:37