I’m working on a Android Studio app using Java. I started working on the database today (SQLite Database) and I’ve created the myDatabaseHelper class.
package com.example.findyourworker;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.widget.Toast;
import androidx.annotation.Nullable;
class MyDatabaseHelper extends SQLiteOpenHelper {
private Context context;
private static final String DATABASE_NAME = "accounts.db";
private static final int DATABASE_VERSION = 1;
private static final String TABLE_NAME = "users_data";
private static final String COLUMN_ID = "_id";
private static final String COLUMN_USERNAME = "username";
private static final String COLUMN_PASSWORD = "password";
private static final String COLUMN_EMAIL = "email";
public MyDatabaseHelper(@Nullable Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
this.context = context;
}
@Override
public void onCreate(SQLiteDatabase db) {
String query ="CREATE TABLE " + TABLE_NAME +
" (" + COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
COLUMN_USERNAME + " TEXT, " +
COLUMN_PASSWORD + " TEXT, " +
COLUMN_EMAIL + " TEXT);";
db.execSQL(query);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
void addAccount(String username,String password,String email) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(COLUMN_USERNAME, username);
cv.put(COLUMN_PASSWORD, password);
cv.put(COLUMN_EMAIL, email);
long result = db.insert(TABLE_NAME, null, cv);
if (result == -1) {
Toast.makeText(context, "Failed", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(context, "Added Successfully", Toast.LENGTH_SHORT).show();
}
}
boolean checkLogin(String email, String password) {
SQLiteDatabase db = this.getReadableDatabase();
String[] columns = {COLUMN_ID};
String selection = COLUMN_EMAIL + "=? AND " + COLUMN_PASSWORD + "=?";
String[] selectionArgs = {email, password};
Cursor cursor = db.query(TABLE_NAME, columns, selection, selectionArgs, null, null, null);
boolean loginSuccessful = cursor != null && cursor.getCount() > 0;
if (cursor != null) {
cursor.close();
}
return loginSuccessful;
}
boolean checkUsernameExists(String username) {
SQLiteDatabase db = this.getReadableDatabase();
String[] columns = {COLUMN_ID};
String selection = COLUMN_USERNAME + "=?";
String[] selectionArgs = {username};
Cursor cursor = db.query(TABLE_NAME, columns, selection, selectionArgs, null, null, null);
boolean usernameExists = cursor != null && cursor.getCount() > 0;
if (cursor != null) {
cursor.close();
}
return usernameExists;
}
boolean checkPasswordExists(String password) {
SQLiteDatabase db = this.getReadableDatabase();
String[] columns = {COLUMN_ID};
String selection = COLUMN_PASSWORD + "=?";
String[] selectionArgs = {password};
Cursor cursor = db.query(TABLE_NAME, columns, selection, selectionArgs, null, null, null);
boolean passwordExists = cursor != null && cursor.getCount() > 0;
if (cursor != null) {
cursor.close();
}
return passwordExists;
}
boolean checkEmailExists(String email) {
SQLiteDatabase db = this.getReadableDatabase();
String[] columns = {COLUMN_ID};
String selection = COLUMN_EMAIL + "=?";
String[] selectionArgs = {email};
Cursor cursor = db.query(TABLE_NAME, columns, selection, selectionArgs, null, null, null);
boolean emailExists = cursor != null && cursor.getCount() > 0;
if (cursor != null) {
cursor.close();
}
return emailExists;
}
}
And this is how I implemented the use of this DATABASE in the register page:
submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Inside onClick, update variables based on input validation
username = un.getText().toString();
password = pass.getText().toString();
email = em.getText().toString();
unValid = !TextUtils.isEmpty(username) && isUsernameValid(username);
passValid = !TextUtils.isEmpty(password) && isValidPassword(password);
emailValid = !TextUtils.isEmpty(email) && isValidEmail(email);
if (unValid && passValid && emailValid) {
// All fields are valid, proceed with the signup process
if (!myDB.checkUsernameExists(username) && !myDB.checkEmailExists(email)) {
myDB.addAccount(username.trim(), password.trim(), email.trim());
Intent intent = new Intent(signup.this, fragments_host.class);
// startActivity(intent);
} else {
if (myDB.checkEmailExists(email) && myDB.checkUsernameExists(username)) {
Toast.makeText(context, "Email and Username Already Exist", Toast.LENGTH_SHORT).show();
} else if (myDB.checkUsernameExists(username)) {
Toast.makeText(context, "Username Already Exists", Toast.LENGTH_SHORT).show();
} else if (myDB.checkEmailExists(email)) {
Toast.makeText(context, "Email Already Exists", Toast.LENGTH_SHORT).show();
}
}
} else {
// Handle invalid input fields here
if (!unValid) {
un.setTextColor(Color.RED);
vibrateDevice(signup.this, 200);
new Handler().postDelayed(() -> un.setTextColor(Color.BLACK), 200);
}
if (!passValid) {
pass.setTextColor(Color.RED);
vibrateDevice(signup.this, 200);
new Handler().postDelayed(() -> pass.setTextColor(Color.BLACK), 200);
}
if (!emailValid) {
em.setTextColor(Color.RED);
vibrateDevice(signup.this, 200);
new Handler().postDelayed(() -> em.setTextColor(Color.BLACK), 200);
}
}
}
});
And in the register it works. My problem is with the login page:
submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (myDB.checkEmailExists(email) && myDB.checkPasswordExists(password)) {
Toast.makeText(context, "Login was Successful.", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(login.this, fragments_host.class);
startActivity(intent);
} else {
Toast.makeText(context, "Account doesn't exist.", Toast.LENGTH_SHORT).show();
}
}
});
If anyone has any idea why the login page database connection is not working please leave a comment or an answer your help would be much appreciated!
I tried running pieces of the code by ChatGPT and everything looks correct. And I tried modifying the code a little and nothing worked. I also tried to locate the DATABASE and watch it live and I couldn’t find it in the folders of my project.
Tamir nab is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.