如何在SQLite数据库(登录和注册系统)中更新密码?

时间:2017-12-06 08:35:20

标签: java android sqlite android-sqlite

当我在更改密码类或布局中单击“提交”按钮时,应用程序崩溃。我的代码出了什么问题,请教我!提前致谢。我正在使用Sqlite数据库用于用户登录系统,用户可以从应用栏布局更改其密码。点击>>在App栏中更改密码时会弹出新活动,即changePassword活动。到目前为止一切正常,但在输入密码并点击提交按钮后,应用程序崩溃并重新回到MainActivity。

这是我的更改密码类

public class changePasswordActivity extends AppCompatActivity {

EditText oldpasswordEditText;
EditText newpasswordEditText;
EditText confirmpasswordEditText;
Button btnsubmit;

String realusername, realpassword;


String checkoldpass, checknewpass, checkconfirmpass;

SQLiteHelper sqLiteHelper;


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

    oldpasswordEditText = findViewById(R.id.oldpasswordEditText);
    newpasswordEditText = findViewById(R.id.newPasswordEditText);
    confirmpasswordEditText = findViewById(R.id.confirmPasswordEditText);
    btnsubmit= findViewById(R.id.btnsubmit);

    checkoldpass = oldpasswordEditText.getText().toString();
    checknewpass = newpasswordEditText.getText().toString();
    checkconfirmpass = confirmpasswordEditText.getText().toString();

    realusername = getIntent().getStringExtra("USERNAME");
    realpassword= getIntent().getStringExtra("PASSWORD");

    btnsubmit.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            if(!(checkconfirmpass.equals(checknewpass))) {
                Toast.makeText(changePasswordActivity.this,"New Password didn't matched",Toast.LENGTH_SHORT).show();

            }else if (!(realpassword.equals(checkoldpass))){
                Toast.makeText(changePasswordActivity.this,"Old password didn't matched",Toast.LENGTH_SHORT).show();
            }
            else if (checkconfirmpass.equals(checkoldpass)){
                Toast.makeText(changePasswordActivity.this,"New password cannot be same as old password",Toast.LENGTH_SHORT).show();

            }else if(checkconfirmpass.equals(checknewpass.equals(checkoldpass))) {


                Toast.makeText(changePasswordActivity.this, "New password cannot be same as old password", Toast.LENGTH_SHORT).show();
            }
            else{
                sqLiteHelper = new SQLiteHelper(changePasswordActivity.this);
                sqLiteHelper.changepassword(checknewpass,realusername);
            }

        }
    });
}
}

这是我的SQLiteHelper类

public class SQLiteHelper extends SQLiteOpenHelper {

SQLiteDatabase db;
private static final String DATABASE_NAME = "info.db";
private static final int DATABASE_VERSION = 1;

public static final String TABLE_NAME = "profile";
public static final String COLUMN_ID = "userid";
public static final String COLUMN_FULLNAME = "fullname";
public static final String COLUMN_EMAIL = "email";
public static final String COLUMN_PASSWORD = "password";
public static final String COLUMN_MOBILE = "mobile";

private static final String CREATE_TABLE_QUERY =
        "CREATE TABLE " + TABLE_NAME + " (" +
                COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                COLUMN_FULLNAME + " TEXT, " +
                COLUMN_EMAIL + " TEXT, " +
                COLUMN_PASSWORD + " TEXT, " +
                COLUMN_MOBILE + " TEXT " + ")";

//modified constructor
public SQLiteHelper(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL(CREATE_TABLE_QUERY);
}

@Override
public void onUpgrade(SQLiteDatabase db, int i, int i1) {
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
    onCreate(db);
}


public Cursor getData() {
    String query = "SELECT * FROM" + TABLE_NAME;

    Cursor data = db.rawQuery(query, null);
    return data;
}

public void changepassword(String mpassword, String mname) {



   Cursor cur= db.rawQuery("UPDATE "+SQLiteHelper.TABLE_NAME+" SET "
           +SQLiteHelper.COLUMN_PASSWORD
           +" = '"+mpassword+"' WHERE "+ SQLiteHelper.COLUMN_EMAIL+" = ?",new String[]{mname});

}
}

1 个答案:

答案 0 :(得分:2)

db 将为null,也不需要Cursor,所以更改

public void changepassword(String mpassword, String mname) {
   Cursor cur= db.rawQuery("UPDATE "+SQLiteHelper.TABLE_NAME+" SET "
           +SQLiteHelper.COLUMN_PASSWORD
           +" = '"+mpassword+"' WHERE "+ SQLiteHelper.COLUMN_EMAIL+" = ?",new String[]{mname});
}

要: -

public void changepassword(String mpassword, String mname) {
   SQLiteDatabase db = this.getWriteableDatabae();
   db.rawQuery("UPDATE "+SQLiteHelper.TABLE_NAME+" SET "
           +SQLiteHelper.COLUMN_PASSWORD
           +" = '"+mpassword+"' WHERE "+ SQLiteHelper.COLUMN_EMAIL+" = ?",new String[]{mname});
}

getData方法也存在同样的问题,因此您也应该将SQLiteDatabase db = this.getWriteableDatabae();添加到getData方法中。

或者你可以改变: -

public SQLiteHelper(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

来: -

public SQLiteHelper(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
    db = this.getWriteableDatabase();
}

在这种情况下,db将是一个有效的SQLiteDatabase。