Error with RawQuery in SQLite

1

Good friends, I made my database to do the registration of users and their login, I used separate classes for the base:

public class Usuario {
    private int id;
    private String nombre;
    private String password;

    public Usuario(int id, String nombre, String password) {
        this.id = id;
        this.nombre = nombre;
        this.password = password;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getNombre() {
        return nombre;
    }

    public void setNombre(String nombre) {
        this.nombre = nombre;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

This is my second class

public class Utilidades {

    //Constantes campos tabla usuario
    public static final String TABLA_USUARIO = "usuario";
    public static final String ID = "id";
    public static final String NOMBRE = "nombre";
    public static final String PASS = "password";

    public static final String CREAR_TABLA_USUARIO="create table " + TABLA_USUARIO+ "("+ID+" INTEGER PRIMARY KEY AUTOINCREMENT," + NOMBRE + "TEXT, " + PASS + "TEXT)";

}

However at the time of verifying the users in the corresponding activity I get an error where the rawQuery is empty. This is what I have in my class where I do the login

public class loggeo extends AppCompatActivity {

    EditText et1,et2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_03_loggeo);

        ConexionSQLiteHelper conn = new ConexionSQLiteHelper(this,"bd_usuarios",null,1);

        et1= (EditText) findViewById(R.id.txtUsrLog);
        et2= (EditText) findViewById(R.id.txtPassLog);
    }

    public void onClick (View view){
        iniciarSesion();
    }

    public void iniciarSesion(){
        Cursor fila;


        ConexionSQLiteHelper conn = new ConexionSQLiteHelper(this,"bd_usuarios",null,1);

        SQLiteDatabase db = conn.getWritableDatabase();

        String usr=et1.getText().toString();
        String pass=et2.getText().toString();

        fila=db.rawQuery("select "+ Utilidades.NOMBRE+", "+Utilidades.PASS+"from "+Utilidades.TABLA_USUARIO+" where "+Utilidades.NOMBRE+"='"+usr+"' and "+Utilidades.PASS+"='"+pass+"'",null);

        if (fila.moveToFirst()){
            String filUs=fila.getString(0);
            String filPass=fila.getString(1);

            if (et1.equals(filUs)&&filPass.equals(pass)){
                Intent ven=new Intent(this,drawer_perfil.class);
                startActivity(ven);
                et1.setText("");
                et2.setText("");
            }
        }
        db.close();

    }
}

I would like you to guide me on where my error is, since I can not really understand it, I hope you can help me.

Edit: Class where I register users

public class registroUsuario extends AppCompatActivity {

    EditText nombre,password;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_02_registro);

        ConexionSQLiteHelper conn = new ConexionSQLiteHelper(this,"bd_usuarios",null,1);

        nombre = (EditText)findViewById(R.id.txtUsuarioNuevo);
        password = (EditText)findViewById(R.id.txtPassNuevo);

    }
    public void onClick (View view){
        registrarUsuarios();
    }

    private void registrarUsuarios() {
        ConexionSQLiteHelper conn = new ConexionSQLiteHelper(this,"bd_usuarios",null,1);

        SQLiteDatabase db = conn.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(Utilidades.NOMBRE,nombre.getText().toString());
        values.put(Utilidades.PASS,password.getText().toString());

        Long enviado = db.insert(Utilidades.TABLA_USUARIO,Utilidades.ID,values);
        Toast.makeText(getApplicationContext(),"Registro satisfactorio",Toast.LENGTH_SHORT).show();
        db.close();

        Intent siguiente = new Intent(registroUsuario.this, loggeo.class);
        startActivity(siguiente);

    }
}
    
asked by Manuel Hdez Galván 01.12.2017 в 10:48
source

1 answer

2

The error is because your Query is incorrect, you are omitting some spaces, that causes the query to return a Cursor with null value, your query is incorrect:

fila=db.rawQuery("select "+ Utilidades.NOMBRE+", "+Utilidades.PASS+"from "+Utilidades.TABLA_USUARIO+" where "+Utilidades.NOMBRE+"='"+usr+"' and "+Utilidades.PASS+"='"+pass+"'",null);

The query that will be incorrectly constructed would be:

"select nombre, passwordfrom usuario where nombre='...' and password='...'"

I recommend you make this change, which actually adds spaces to correctly construct the query:

fila=db.rawQuery("select "+ Utilidades.NOMBRE+", "+Utilidades.PASS+" from "+Utilidades.TABLA_USUARIO+" where "+Utilidades.NOMBRE+"='"+usr+"' and "+Utilidades.PASS+"='"+pass+"'",null );

    
answered by 01.12.2017 / 17:27
source