基于旧密码更新密码。 SQLite数据库Android

时间:2018-12-12 05:16:25

标签: android sqlite

我正在尝试根据保存在SQLite数据库中的旧密码来更改/更新密码,但是它不起作用

这是Model类:

public class SyncDownPasswordsTable {

public static final String SYNCDOWN_PASSWORDS_TABLE_NAME = "SyncDownPasswordsTable";
public static final String KEY_USER_ID = "userId";
public static final String KEY_PASSWORD = "password";

private String userId , password;

public SyncDownPasswordsTable(String userId, String password) {
    this.userId = userId;
    this.password = password;
}

public SyncDownPasswordsTable() {
}

public String getUserId() {
    return userId;
}

public void setUserId(String userId) {
    this.userId = userId;
}

public String getPassword() {
    return password;
}

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

public static String CreateSyncDownPasswordTable = "CREATE TABLE IF NOT EXISTS " + SYNCDOWN_PASSWORDS_TABLE_NAME + " ( " +
        " " + KEY_USER_ID + " integer primary key AUTOINCREMENT," +
        " " + KEY_PASSWORD + " text" + ")";
}

和数据库处理程序类中的方法。

public int updateSyncDownPassword(SyncDownPasswordsTable synDPTable) {
    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues values = new ContentValues();
    values.put(SyncDownPasswordsTable.KEY_PASSWORD, synDPTable.getPassword());

    // updating row
    return db.update(SyncDownPasswordsTable.SYNCDOWN_PASSWORDS_TABLE_NAME, values, SyncDownPasswordsTable.KEY_USER_ID + " = ?",
            new String[]{String.valueOf(synDPTable.getUserId())});
}

和UpdatePassword对话框中的方法调用。

if (v == updateButton) {
        if (!(oldPass.equals(savedPass) && newPass.equals(confirmPass))) {

            toast = Toast.makeText(getContext(), "Passwords didn't match , check Again", Toast.LENGTH_SHORT);
            toast.setGravity(Gravity.CENTER, 0, 0);
            toast.show();
            oldPassword.setText("");
            newPassword.setText("");
            confirmPassword.setText("");
        } else {

            SyncDownPasswordsTable syncDownPasswordsTable = new SyncDownPasswordsTable();
            syncDownPasswordsTable.setUserId(syncDownPasswordsTable.getUserId());
            syncDownPasswordsTable.setPassword(newPass);
            int update = dbHandler.updateSyncDownPassword(syncDownPasswordsTable);

            if (update > 0) {
                toast = Toast.makeText(getContext(), "Password Updated Successfully .", Toast.LENGTH_SHORT);
                toast.setGravity(Gravity.CENTER, 0, 0);
                toast.show();
                dismiss();
            } else {
                toast = Toast.makeText(getContext(), "Error Occured", Toast.LENGTH_SHORT);
                toast.setGravity(Gravity.CENTER, 0, 0);
                toast.show();
                dismiss();
            }
        }
    } else {
        dismiss();

1 个答案:

答案 0 :(得分:0)

我相信您正在获取/传递的用户ID为“”,而不是当前的用户ID。

那是

SyncDownPasswordsTable syncDownPasswordsTable = new SyncDownPasswordsTable();

创建一个新的SyncDownPasswordsTable对象,未设置任何值/属性。

然后是

syncDownPasswordsTable.setUserId(syncDownPasswordsTable.getUserId());

将用户ID设置为syncDownPasswordsTable.getUserId();

获得的现有未设置用户ID

简而言之,它对值不起作用。

因此,您需要按照:-

syncDownPasswordsTable.setUserId(a_valid_existing_current_userid);

其中 a_valid_existing_current_userid 是您要更新的用户ID。