SQLite query

1

When I want to ask a question, the program closes. I attach the code:

public void consulta (View v){
        AdminSQLiteOpenHelper admin= new AdminSQLiteOpenHelper(this, "administracion", null, 1);
        SQLiteDatabase BaseDeDatos=admin.getWritableDatabase();

        String nombre=et2.getText().toString(); 

    if(nombre.length()!=0){
        Cursor fila=BaseDeDatos.rawQuery("SELECT nombre,codigo,precio,genero FROM articulos WHERE nombre="+ nombre ,null);
        if (fila.moveToFirst()){
            et2.setText(fila.getString(0));
            et1.setText(fila.getString(1));
            et4.setText(fila.getString(2));
            et3.setText(fila.getString(3));
            BaseDeDatos.close();
        }else {
            Toast.makeText(this, "No existe dicha persona", Toast.LENGTH_SHORT).show();
            BaseDeDatos.close();
        }   
    } else{
         Toast.makeText(this, "campo vacio", Toast.LENGTH_SHORT).show();
    }

}

Deputy LogCat:

    
asked by Ibarra Emiliano 14.06.2018 в 01:36
source

1 answer

0

LogCat is always important, we can easily find the problem, in this case the problem is:

  

Caused by: android.database.sqlite.SQLiteExcetion: no such column:   emiliano (code 1):, while compiling: SELECT name,   code, price, generofrom articles WHERE name = emiliano

You can solve this error by defining the value of the field in single quotes, so that you do not believe that the value is a field since you are creating a query in this way:

 SELECT nombre, codigo,precio,generofrom articulos WHERE nombre=emiliano

should be like this:

 SELECT nombre, codigo,precio,generofrom articulos WHERE nombre='emiliano'

in this way you can solve it in your code:

Cursor fila=BaseDeDatos.rawQuery("SELECT nombre,codigo,precio,genero FROM articulos WHERE nombre='"+ nombre +"'" ,null);
    
answered by 14.06.2018 в 16:36