Comparison of values with SELECT

Asked

Viewed 624 times

2

Hello, I am creating a search project and I need to make a confirmation in my database.

I have a table "establishments" in my Sqlite database, with the following columns: id, name and category. And a String type variable called "naming"

I need to make a comparison between the value of the variable and the database data, IE, I need to check if inside the column name there is some field that has the same value of the variable, if there is a return of a True for example, otherwise, return a False.

Follows the codes..

SQL code

    SQLiteDatabase db = openOrCreateDatabase("pesquisa.db", Context.MODE_PRIVATE, null);

    StringBuilder sqlEstabelicimento = new StringBuilder();
    sqlEstabelicimento.append("CREATE TABLE IF NOT EXISTS [estabelecimentos](");
    sqlEstabelicimento.append("[_id] INTEGER PRIMARY KEY AUTOINCREMENT, ");
    sqlEstabelicimento.append("nm_estab VARCHAR(100), ");
    sqlEstabelicimento.append("categoria VARCHAR(50));");

    db.execSQL(sqlEstabelicimento.toString());

Java code

    public void pesquisar(View view){
    SQLiteDatabase db = openOrCreateDatabase("pesquisa.db", Context.MODE_PRIVATE, null);

    EditText nomeDigitado = (EditText) findViewById(R.id.txt_NomePesquisa);
    String texto = nomeDigitado.getText().toString();

    try{
     db.execSQL("SELECT nm_estab FROM estabelecimentos WHERE nm_estab = '"+texto+"'");
    }
    catch (Exception erro){

    }
}
  • Have you assembled your SQL? Post it for the staff help you and post also your code in Java.

  • This question is not so clear. Will the variable "nameEstab" be the comparison parameter with the persisted object? If yes, will comparison be made with which attributes of the object persisted in the database? With all attributes?

1 answer

2

This is the SELECT:

SELECT * FROM estabelecimentos WHERE nome LIKE '%suaVariavel%'
  • 1

    Thank you, it worked perfectly

  • nothing! if this was the expected answer, identify it so that other users with the same doubt can view the solution....

Browser other questions tagged

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