0

I've been trying to update the password for a specific user on my android app, but it just won't work. It gives me a "Password Updated Successfully" message but in the SQLite database, it doesn't change at all. I'm not sure what's wrong, my code is below.

//Database.java

public long updatePassword(String password, String email) {
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put("password", password);
    return db.update("users", values, email+ "= ?" , new String[]{email});
    //db.close();
}

//ConfirmPassword.java below

        resetpass.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String val1 = pass1.getText().toString();
            String val2 = pass2.getText().toString();

            if (val1.isEmpty() && val2.isEmpty()) {
              Toast.makeText(getApplicationContext(), "Fields are Empty", Toast.LENGTH_SHORT).show();
              return;
            }

            if (!val1.contentEquals(val2)) {
                Toast.makeText(getApplicationContext(), "Password doesn't match. Try again!", 
                                  Toast.LENGTH_SHORT).show();
                pass1.setText("");
                pass2.setText("");
                return;
            }

            else {
                //db.open();
                db.updatePassword(val1, email); //email here throws an error since there is no field and email declaration in this file
                db.close();
                Toast.makeText(getApplicationContext(), "Password Updated Successfully", 
                           Toast.LENGTH_SHORT).show();
                openLogin();
            }
        }
    });
nodisplay
  • 1
  • 3
  • `db.updatePassword(val1, val2);` which param is email ?? seems like both are password – IntelliJ Amiya Apr 09 '20 at 07:37
  • you need to check what is returned by db.update(), for success it should be greater then 0. If not then you need to check you parameters and table name – Waqar UlHaq Apr 09 '20 at 07:40
  • Check the return type of public long updatePassword(String password, String email), if a long value greater than 0, means any row get updated with matched data. Also, debug and check that you are passing correct info of passowrd and email. – Ravi Apr 09 '20 at 07:43
  • I tried passing email in the parameters but it returned an error, since theres no email field and variable as such in that ConfrimPassword.java file `db.updatePassword(val1, email); db.close(); Toast.makeText(getApplicationContext(), "Password Updated Successfully", Toast.LENGTH_SHORT).show(); openLogin(); ` – nodisplay Apr 09 '20 at 07:47

0 Answers0