簡體   English   中英

Android中的SQLite如何更新值

[英]SQLite in Android How to update values

據我研究,有兩種方法可以更新SQLite數據庫

execSQL(String sql) method

或者:

update(String table, ContentValues values, String whereClause, String[] whereArgs) method.

在我的應用程序中,我正在使用update方法選項。

基本上我想實現忘記密碼的功能。 另外,我已經將用戶名作為數據庫中的主鍵,即,沒有兩個用戶可以使用相同的用戶名。 因此,當用戶點擊忘記的密碼時,他輸入了正確的用戶名,如果正確,則輸入了新密碼,並且應該替換數據庫中的舊密碼。

這是我定義的更新密碼的方法

public void updateEntry(String userName, String password) {
    ContentValues updatedValues = new ContentValues();
    updatedValues.put("PASSWORD", password);

    String where = "USERNAME = ?";
    db.update("LOGIN", updatedValues, where, new String[] { userName });
}

這就是我如何調用updateEntry方法

 public void onClick(View v) {
            String userName=forgot_username.getText().toString();
            String funame=loginDataBaseAdapter.getUserName(userName);
            String password=forgot_password.getText().toString();
            if (userName.equals(funame)) {
                if (forgot_password.equals(forgot_confirmpassword)){
                    loginDataBaseAdapter.updateEntry(userName,password);
                }

                Toast.makeText(ForgotPasswordActivity.this,"success",Toast.LENGTH_SHORT).show();
            }else {
                Toast.makeText(ForgotPasswordActivity.this,"User Name is not correct",Toast.LENGTH_SHORT).show();

            }
        }

我是SQLite的新手,無法解決問題。 感謝所有幫助。

嘗試這個:

將此方法添加到LoginDataBaseAdapter類中

public boolean updateEntry(String userName, String password) {
  ContentValues args = new ContentValues();
  args.put("PASSWORD", password);
  return db.update("LOGIN", args, "USERNAME=" + userName, null) > 0;
}

更新您的onClick方法:

public void onClick(View v) {
    String userName = forgot_username.getText().toString();
    String funame = loginDataBaseAdapter.getUserName(userName);
    String password = forgot_password.getText().toString();
    if (userName.equals(funame)) {
        if (forgot_password.equals(forgot_confirmpassword)) {
            if (loginDataBaseAdapter.updateEntry(userName,password)) {
                Toast.makeText(ForgotPasswordActivity.this, "success", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(ForgotPasswordActivity.this,"failed",Toast.LENGTH_SHORT).show();
            }
        }
    } else {
        Toast.makeText(ForgotPasswordActivity.this,"User Name is not correct",Toast.LENGTH_SHORT).show();
    }
}

讓我們知道顯示了哪個Toast消息。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM