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

Asked

Viewed 76 times

0

I want to search using the "Clas" field, which is a String. And listPsearch gets 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;
    }
  • The answers below help you or need some more information?!

2 answers

3

If you go through the column nome, can do this way using the operator =:

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 the LIKE. See this question about what difference between operator '=' and LIKE.

How you’re doing a column search clas, you can do it this way:

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

[...] the LIKE searches for "something like", ie content that has the text searched in a part of where (column(s)) you are looking for. In general use % symbol to indicate where it can have characters joker, where there can be anything else. Maniero

1

Modify the line that uses the "search" parameter in this way:

String where = "clas like '%" + pesquisa + "%'"; 

Browser other questions tagged

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