更新不在sqlite android中工作

时间:2016-05-08 14:11:36

标签: android sqlite android-studio login android-sqlite

我有登录和重置密码活动。当我输入新的更新密码并尝试再次登录时,我无法使用新密码。使用旧密码登录可以正常工作。基本上,密码字段不会被更新/覆盖。

logcat中没有错误。只是密码没有更新。

请帮忙,因为我是Android开发的新手。

更新代码(DataRegister是具有GET和SET功能的类):

 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});


}

检索代码:

 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


}

登录代码:

 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();


                }



        }
    });

重置密码代码:

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();

            }


        }

    });

1 个答案:

答案 0 :(得分:0)

也许这可以帮助您更新数据:

  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是扩展DAOBase的类的名称,NAME_TABLE是"密码",您需要创建一个id登录名和密码的类密码。

希望这可以帮到你