Question
Andriod Studio--Using JAVA and SQLite database I am having trouble creating a update/change password. Can someone please take a look and help? I included all
Andriod Studio--Using JAVA and SQLite database
I am having trouble creating a update/change password. Can someone please take a look and help? I included all the code so you could see what I was trying to do. I am having problems in my DatabaseHelper--my Cursor getData() and changepassword are not picking anything up. My changePass 'else' is not correct. What I want for the code to do is check the email address and password before letting the user change/update the password. The login and MainActivity work. Thanks for you time
Registration- Main Activity
MainActivity.java_________________________________________________________________________
public class MainActivity extends AppCompatActivity { SQLiteOpenHelper openHelper; SQLiteDatabase myDb2; EditText editFName, editLName, editEmail, editPassword; Button reg_btn, Login_btn, btnViewAll; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); openHelper = new DatabaseHelper(this); editFName = (EditText)findViewById(R.id.editFName); editLName = (EditText)findViewById(R.id.editLName); editEmail = (EditText)findViewById(R.id.editEmail); editPassword = (EditText)findViewById(R.id.editPassword); Login_btn = (Button)findViewById(R.id.Login_btn); btnViewAll = (Button)findViewById(R.id.btnViewAll); reg_btn = (Button)findViewById(R.id.reg_Btn); reg_btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { myDb2=openHelper.getWritableDatabase(); String fName = editFName.getText().toString(); String lName = editLName.getText().toString(); String email = editEmail.getText().toString(); String password = editPassword.getText().toString(); insertData(fName, lName, email, password); Toast.makeText(MainActivity.this,"Data Inserted",Toast.LENGTH_LONG).show(); } }); Login_btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(MainActivity.this, login.class); startActivity(intent); } }); } public void insertData(String fName, String lName, String email, String password){ ContentValues values = new ContentValues(); values.put(DatabaseHelper.FNAME, fName); values.put(DatabaseHelper.LNAME, lName); values.put(DatabaseHelper.EMAIL, email); values.put(DatabaseHelper.PASSWORD, password); long id = myDb2.insert(DatabaseHelper.TABLE_USER, null, values); myDb2.close(); } }
_______________________________________________
login.java
public class login extends AppCompatActivity { SQLiteOpenHelper openHelper; SQLiteDatabase myDb; EditText editLogin, editPass; Button btnLogin, btnChangePass; Cursor cursor; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login); openHelper=new DatabaseHelper(this); myDb=openHelper.getReadableDatabase(); btnLogin=(Button)findViewById(R.id.btnLogin); btnChangePass=(Button)findViewById(R.id.btnChangePass); editLogin=(EditText)findViewById(R.id.editLogin); editPass=(EditText)findViewById(R.id.editPass); btnLogin.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String login = editLogin.getText().toString(); String pass = editPass.getText().toString(); cursor = myDb.rawQuery(" SELECT * FROM "+ DatabaseHelper.TABLE_USER + " WHERE " + DatabaseHelper.EMAIL + " =? AND " + DatabaseHelper.PASSWORD+ " =? ", new String[]{login, pass}); if(cursor !=null){ if(cursor.getCount()>0){ Toast.makeText(getApplicationContext(), "login successful", Toast.LENGTH_LONG).show(); }else { Toast.makeText(getApplicationContext(),"error", Toast.LENGTH_LONG).show(); } } } }); btnChangePass.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(login.this, changePass.class); startActivity(intent); } }); } }
______________________________________________________________
DatabaseHelper.java
public class DatabaseHelper extends SQLiteOpenHelper { SQLiteDatabase db; public static final String DATABASE_NAME = "Test.db"; //columns for User table public static final String TABLE_USER = "user_table"; public static final String USER_ID = "USER_ID"; public static final String FNAME = "FNAME"; public static final String LNAME = "LNAME"; public static final String EMAIL = "EMAIL"; public static final String PASSWORD = "PASSWORD"; private static final String CREATE_TABLE_QUERY = "CREATE TABLE " + TABLE_USER + " (" + USER_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + FNAME + " TEXT, " + LNAME + " TEXT, " + EMAIL + " TEXT, " + PASSWORD + " TEXT " + ")"; public DatabaseHelper(Context context) { super(context, DATABASE_NAME, null, 1); db=this.getWritableDatabase(); } @Override public void onCreate(SQLiteDatabase db) { db.execSQL(CREATE_TABLE_QUERY); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { db.execSQL("DROP TABLE IF EXISTS " + TABLE_USER); //db.execSQL("DROP TABLE IF EXISTS " + TABLE_WO_DETAIL); //db.execSQL("DROP TABLE IF EXISTS " + TABLE_EXERCISE); // db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME); onCreate(db); } public Cursor getData() { String query = "SELECT * FROM " + TABLE_USER; Cursor data = db.rawQuery(query, null); return data; }
public void changepassword (String mpassword, String memail){ db.rawQuery("UPDATE " + DatabaseHelper.TABLE_USER + " SET " + DatabaseHelper.PASSWORD + " = '" + mpassword + "' WHERE " + DatabaseHelper.EMAIL + " = ?", new String[]{memail}); } }
_________________________________________________
changePass.java
public class changePass extends AppCompatActivity { SQLiteOpenHelper openHelper; SQLiteDatabase myDb2; EditText editCurPass; EditText editNewPass; EditText editConfirmPass; Button btnSubmit; String realemail, realpassword; String checkoldpass, checknewpass, checkconfirmpass; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_change_pass); openHelper = new DatabaseHelper(this); myDb2=openHelper.getWritableDatabase(); editCurPass = findViewById(R.id.editCurPass); editNewPass = findViewById(R.id.editNewPass); editConfirmPass = findViewById(R.id.editConfirmPass); btnSubmit= findViewById(R.id.btnSubmit); checkoldpass = editCurPass.getText().toString(); checknewpass = editNewPass.getText().toString(); checkconfirmpass = editConfirmPass.getText().toString(); realemail = getIntent().getStringExtra("EMAIL"); realpassword= getIntent().getStringExtra("PASSWORD"); btnSubmit.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if(!(checkconfirmpass.equals(checknewpass))) { Toast.makeText(changePass.this,"New Password didn't matched", Toast.LENGTH_SHORT).show(); }else if (!(realpassword.equals(checkoldpass))){ Toast.makeText(changePass.this,"Old password didn't matched",Toast.LENGTH_SHORT).show(); } else if (checkconfirmpass.equals(checkoldpass)){ Toast.makeText(changePass.this,"New password cannot be same as old password",Toast.LENGTH_SHORT).show(); }else if(checkconfirmpass.equals(checknewpass.equals(checkoldpass))) { Toast.makeText(changePass.this, "New password cannot be same as old password", Toast.LENGTH_SHORT).show(); } else{ myDb2 = new SQLiteOpenHelper(changePass.this); //error myDb2.changepassword(checknewpass,realemail); //error } } }); } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started