简体   繁体   中英

SQLite Table is inserting but not updating

The data in the first table is getting updated while data in the second table is not updating. Please help to update all the tables. I attach the Database Helper class and java coding here. The below code is for updating table 2 which is for forgot password and to update the new password of admin. The new password of user is only getting updated. I get a message "Password Updated" but when I login it says "Invalid credentials" since the table is not updated.

Thanks in advance:)

     MyDB.execSQL("create Table users(Fullname TEXT NOT NULL PRIMARY KEY, Username TEXT NOT NULL, Password TEXT NOT NULL, Email TEXT NOT NULL, Phoneno TEXT NOT NULL, Address TEXT NOT NULL, City TEXT NOT NULL, Pincode TEXT NOT NULL)");

     MyDB.execSQL("create Table admin(ID INTEGER PRIMARY KEY AUTOINCREMENT,A_username TEXT NOT NULL, A_email TEXT NOT NULL, A_password TEXT NOT NULL)");

 @Override
 public void onUpgrade(SQLiteDatabase MyDB, int i, int i1) {
     MyDB.execSQL(" DROP TABLE IF EXISTS users");
     MyDB.execSQL(" DROP TABLE IF EXISTS admin");
     onCreate(MyDB);

 }
 public boolean updateDataAdmin(String A_username, String A_password)
 {
     SQLiteDatabase MyDB=this.getWritableDatabase();
     ContentValues table2=new ContentValues();
     table2.put(TABLE2_COL_3,A_username);
     int result = MyDB.update("admin", table2, "A_username=?",new String[] {A_username});
     if (result == -1) return false;
     else
         return true;
 }

MainActivity code:

       @Override
        public void onClick(View v) {

            String user = username.getText().toString();
            String Password = password.getText().toString();
            String repass = retype.getText().toString();

            if (Password.isEmpty() || repass.isEmpty()) {
                Toast.makeText(AdminReset.this, "Fields cannot be empty", Toast.LENGTH_SHORT).show();
            }else{
                if (Password.equals(repass)) {

                    Boolean updateadminPass = DB.updateDataAdmin(user, Password);
                    if (updateadminPass == true) {
                        Intent intent = new Intent(getApplicationContext(), AdminLogin.class);
                        startActivity(intent);
                        Toast.makeText(AdminReset.this, "Password Updated Successfully", Toast.LENGTH_SHORT).show();
                    } else {
                        Toast.makeText(AdminReset.this, "Password Not Updated", Toast.LENGTH_SHORT).show();
                    }
                } else {
                    Toast.makeText(AdminReset.this, "Passwords does not match!", Toast.LENGTH_SHORT).show();
                }

            }


        }
    }); 

In your updateDataAdmin() method you're not doing anything with the password value you pass in.

To update it to the database, add it to the ContentValues you're passing to update() , eg

table2.put("A_password", A_password);

(Also consider Anonymoys feedback about naming conventions. It makes things easier for yourself and others.)

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