简体   繁体   中英

UPDATE not working in sqlite android

I have a login and reset password activity. When I enter the new updated password and try to login again, I cannot do so with the new password. Logging in with the old password works fine. Basically, the password field is not getting updated/overwritten.

There is no error in the logcat. Just that the password is not updated.

Please help as I am new to android development.

Code for update( DataRegister is the class with GET AND SET functions):

 public int updatePassword(DataRegister dataregister) {

  db = dbHelper.getWritableDatabase();

    ContentValues updated = new ContentValues();
    updated.put("PASSWORD", dataregister.getPASSWORD());

 return db.update(DataRegister.TABLE, updated, "EMAIL=?" , new String[]{email});


}

Code for retrieval:

 public String getPass(DataRegister dataRegister) {

    db = dbHelper.getWritableDatabase();

    Cursor cursor = db.query(DataRegister.TABLE, null, "EMAIL=?",
            new String[]{dataRegister.getEMAIL()}, null, null, null, null);
    if (cursor != null && cursor.moveToFirst())

    {
        pass = cursor.getString(cursor.getColumnIndex("PASSWORD"));
        cursor.close();
    }
    return pass;


    // return contact


}

Code for Login:

 String email = editTextUserName.getText().toString();
            dataRegister.setEMAIL(email);

            String password = editTextPassword.getText().toString();
            dataRegister.setPASSWORD(password);

            String storedPassword = loginDataBaseAdapter.getSinlgeEntry(dataRegister);

            Toast.makeText(Login.this, storedPassword,Toast.LENGTH_LONG).show();
            Boolean a=loginDataBaseAdapter.isExist(dataRegister.getEMAIL());
           validation = getSharedPreferences("myShaPreferences", Context.MODE_PRIVATE);

            if (password.equals(storedPassword)) {

                Toast.makeText(Login.this,
                        "Congrats: Login Successful", Toast.LENGTH_LONG)
                        .show();
            }

            else {

                    Toast.makeText(Login.this,
                            "User Name or Password does not match",
                            Toast.LENGTH_LONG).show();


                }



        }
    });

Code for reset password:

public class ResetPassword extends AppCompatActivity {


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

    email = (EditText) findViewById(R.id.em2);
    dataRegister=new DataRegister();

    loginDataBaseAdapter = new DatabaseAdapter(this);
    loginDataBaseAdapter = loginDataBaseAdapter.open();



    pass = (EditText) findViewById(R.id.text12);
    conpass = (EditText) findViewById(R.id.text13);

    email1 = email.getText().toString();
    dataRegister.setEMAIL(email1);
    pass1 = pass.getText().toString();

    conpass1 = conpass.getText().toString();
    dataRegister.setPASSWORD(conpass1);

    Button btnReset = (Button) findViewById(R.id.btnReset);
    btnReset.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {


            if (pass1.equals(conpass1)) {

             loginDataBaseAdapter.updatePassword(email1,pass1);
                String newpass = loginDataBaseAdapter.getPass(dataRegister);



                Toast.makeText(ResetPassword.this, newpass, Toast.LENGTH_LONG).show();
                Intent intent = new Intent(ResetPassword.this, Login.class);
                startActivity(intent);


                finish();

            }

            else {
                Toast.makeText(ResetPassword.this,
                        "Password does not match",
                        Toast.LENGTH_LONG).show();

            }


        }

    });

Maybe this can help you for updating data :

  public void updating( Password password) {
        ContentValues value = new ContentValues();

        value.put( PasswordDAO.LOGIN, password.getlogin() );
        value.put( PasswordDAO.PASSWORD, password.getPassword());

        this.mDb.update( PasswordDAO.NAME_TABLE, value, PasswordDAO.ID + " = ?", new String[]{String.valueOf(password.getId())} );
    }

PasswordDao is the name of the class who extends the DAOBase, NAME_TABLE is "Password", and you need to create a class password with id login and password.

hope this can help you

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM