0

i basically want to use the query select password from Login_table where username ='this will be given by the user';

Cursor res =db.rawQuery("select password from Login_table where username ='"+x+"'",null);

i guess this is right but still getting a problem

android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 2

public void checkData(){
        b1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String user_name=usname.getText().toString();
                Cursor res =mydb.getData(user_name);
                if(res.getCount()==0){
                    Toast.makeText(MainActivity.this,"Login failed",Toast.LENGTH_SHORT);
                }
                String check=res.getString(2);
                Toast.makeText(MainActivity.this,String.valueOf(check),Toast.LENGTH_SHORT).show();
                String pass_Word=pass.getText().toString();

                if(pass_Word.compareTo(check)==0){
                    Toast.makeText(MainActivity.this,"You are Loged IN",Toast.LENGTH_LONG).show();
                }
                else
                    Toast.makeTextenter code here(MainActivity.this,"You are Not Loged IN",Toast.LENGTH_SHORT).show();

            }
        });
    }

i just want to retrieve the password and check with the users inputenter code here

Asad Ali Choudhry
  • 4,985
  • 4
  • 31
  • 36
Souvik De
  • 58
  • 1
  • 8
  • Have a look at my answer provided here - https://stackoverflow.com/a/44884971/5550161 – HB. Jun 11 '19 at 14:48

1 Answers1

0

The reason why you are getting the index out of bounds is that you are trying to read data from the position that is BEFORE THE FIRST ROW i.e. -1. You have to move to a row to read data from the row.

So before the line String check=res.getString(2); you need to move to a row, probably using the Cursor's moveToFirst method (although according to the message there are 2 rows returned, which is an issue you would probably want to address as it would appear that there are two rows for the same user and perhaps 2 different passwords).

As the moveToFirst method returns a boolean, true if the move could be made, else false, then there is no need to check to see if there are any rows as if there are not then moveToFirst will return false.

Although probably not currently an issue. You should also always close a Cursor when done with it.

As such you may wish to try using :-

public void checkData(){
    final DBAssetHelper mydb;
    b1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            String user_name=usname.getText().toString();
            Cursor res =mydb.getData(user_name);
            if (res.moveToFirst()) { //<<<<<<<<<< try to move to a row
                Toast.makeText(MainActivity.this,"Login failed", Toast.LENGTH_SHORT);
            } else {
                if (res.getString(2).equals(pass.getText().toString)) {
                    Toast.makeText(MainActivity.this,"You are Loged IN",Toast.LENGTH_LONG).show();
                } else {
                    Toast.makeText(MainActivity.this,"You are Not Loged IN",Toast.LENGTH_SHORT).show();
                }
            }
            res.close(); //<<<<<<<<<< Close the Cursor
        }
    });
}
MikeT
  • 51,415
  • 16
  • 49
  • 68