19-20.SQLite Database create update delete profile.
package com.example.splashscreen.Database;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import androidx.annotation.Nullable;
public class DBHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "AppUser.db";
private static final int DATABASE_VERSION = 2;
private static final String TAG = "DBHelper";
public static final String TABLE_USERS = "users";
public static final String COLUMN_ID = "id";
public static final String COLUMN_NAME = "name";
public static final String COLUMN_EMAIL = "email";
public static final String COLUMN_USERNAME = "username";
public static final String COLUMN_PHONE = "phone";
public static final String COLUMN_PASSWORD = "password";
public static final String COLUMN_BIO = "bio";
public static final String COLUMN_LOCAL_IMAGE_URI = "local_image_uri";
private static final String CREATE_TABLE_USERS = "CREATE TABLE " + TABLE_USERS + "(" +
COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
19-20.SQLite Database create update delete profile.
COLUMN_NAME + " TEXT," +
COLUMN_EMAIL + " TEXT UNIQUE," +
COLUMN_USERNAME + " TEXT UNIQUE," +
COLUMN_PHONE + " TEXT," +
COLUMN_PASSWORD + " TEXT," +
COLUMN_BIO + " TEXT," +
COLUMN_LOCAL_IMAGE_URI + " TEXT" +
")";
public static final String TABLE_SETTINGS = "settings";
public static final String COLUMN_SETTING_KEY = "setting_key";
public static final String COLUMN_SETTING_VALUE = "setting_value";
private static final String CREATE_TABLE_SETTINGS = "CREATE TABLE " + TABLE_SETTINGS +
"(" +
COLUMN_SETTING_KEY + " TEXT PRIMARY KEY," +
COLUMN_SETTING_VALUE + " TEXT" +
")";
public boolean updateUserProfileDetail(String loggedInUserEmail, String columnName,
String newName) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
if (!columnName.equals(COLUMN_NAME) &&
!columnName.equals(COLUMN_USERNAME) &&
!columnName.equals(COLUMN_PHONE) &&
!columnName.equals(COLUMN_BIO)) {
Log.e(TAG, "Invalid column name for update: " + columnName);
return false;
19-20.SQLite Database create update delete profile.
values.put(columnName, newName);
String whereClause = COLUMN_EMAIL + " = ?";
String[] whereArgs = {loggedInUserEmail};
int rowsAffected = 0;
try {
rowsAffected = db.update(TABLE_USERS, values, whereClause, whereArgs);
} catch (Exception e) {
Log.e(TAG, "Error updating user profile detail (" + columnName + ") for " +
loggedInUserEmail, e);
return false;
return rowsAffected > 0;
public static class EmailAlreadyExistsException extends Exception {
public EmailAlreadyExistsException(String message) {
super(message);
public static class UsernameAlreadyExistsException extends Exception {
public UsernameAlreadyExistsException(String message) {
super(message);
}
19-20.SQLite Database create update delete profile.
public DBHelper(@Nullable Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
@Override
public void onCreate(SQLiteDatabase db) {
Log.d(TAG, "Creating tables...");
db.execSQL(CREATE_TABLE_USERS);
db.execSQL(CREATE_TABLE_SETTINGS);
Log.d(TAG, "Tables created.");
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w(TAG, "Upgrading database from version " + oldVersion + " to " + newVersion);
if (oldVersion < 2) {
try {
db.execSQL("ALTER TABLE " + TABLE_USERS + " ADD COLUMN " + COLUMN_BIO + "
TEXT;");
db.execSQL("ALTER TABLE " + TABLE_USERS + " ADD COLUMN " +
COLUMN_LOCAL_IMAGE_URI + " TEXT;");
Log.i(TAG, "Added BIO and LOCAL_IMAGE_URI columns to users table.");
} catch (Exception e) {
Log.e(TAG, "Error adding columns to users table during upgrade", e);
}
19-20.SQLite Database create update delete profile.
public boolean checkEmailExists(String email) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = null;
try {
cursor = db.query(TABLE_USERS, new String[]{COLUMN_ID}, COLUMN_EMAIL + " = ?",
new String[]{email}, null, null, null);
return cursor != null && cursor.moveToFirst();
} finally {
if (cursor != null) cursor.close();
public boolean checkUsernameExists(String username) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = null;
try {
cursor = db.query(TABLE_USERS, new String[]{COLUMN_ID}, COLUMN_USERNAME + "
= ?", new String[]{username}, null, null, null);
return cursor != null && cursor.moveToFirst();
} finally {
if (cursor != null) cursor.close();
public boolean registerUser(String name, String email, String username, String phone,
String password)
19-20.SQLite Database create update delete profile.
throws EmailAlreadyExistsException, UsernameAlreadyExistsException {
if (checkEmailExists(email))
throw new EmailAlreadyExistsException("Email '" + email + "' already registered.");
if (checkUsernameExists(username))
throw new UsernameAlreadyExistsException("Username '" + username + "' already
taken.");
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(COLUMN_NAME, name);
values.put(COLUMN_EMAIL, email);
values.put(COLUMN_USERNAME, username);
values.put(COLUMN_PHONE, phone);
values.put(COLUMN_PASSWORD, password);
values.put(COLUMN_BIO, "");
values.put(COLUMN_LOCAL_IMAGE_URI, "");
long result = -1;
try {
result = db.insertOrThrow(TABLE_USERS, null, values);
} catch (android.database.sqlite.SQLiteConstraintException e) {
Log.e(TAG, "Constraint violation during registration (email/username unique). Email: "
+ email + ", Username: " + username, e);
if (checkEmailExists(email))
throw new EmailAlreadyExistsException("Email '" + email + "' already registered
(caught by constraint).");
if (checkUsernameExists(username))
throw new UsernameAlreadyExistsException("Username '" + username + "' already
taken (caught by constraint).");
19-20.SQLite Database create update delete profile.
return false;
} catch (Exception e) {
Log.e(TAG, "Error registering user. Email: " + email, e);
return false;
return result != -1;
public boolean checkUserCredentials(String emailOrUsername, String password) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = null;
String selection;
String[] selectionArgs = {emailOrUsername, password};
if (emailOrUsername.contains("@")) {
selection = COLUMN_EMAIL + " = ? AND " + COLUMN_PASSWORD + " = ?";
} else {
selection = COLUMN_USERNAME + " = ? AND " + COLUMN_PASSWORD + " = ?";
try {
cursor = db.query(TABLE_USERS, new String[]{COLUMN_ID}, selection, selectionArgs,
null, null, null);
return cursor != null && cursor.moveToFirst();
} finally {
if (cursor != null) cursor.close();
}
19-20.SQLite Database create update delete profile.
public Cursor getUserProfileDetails(String email) {
SQLiteDatabase db = this.getReadableDatabase();
String[] columns = {
COLUMN_NAME,
COLUMN_EMAIL,
COLUMN_USERNAME,
COLUMN_PHONE,
COLUMN_BIO,
COLUMN_LOCAL_IMAGE_URI
};
String selection = COLUMN_EMAIL + " = ?";
String[] selectionArgs = {email};
Cursor cursor = db.query(TABLE_USERS, columns, selection, selectionArgs, null, null,
null);
if (cursor != null) {
Log.d(TAG, "getUserProfileDetails for " + email + ": cursor count " + cursor.getCount());
} else {
Log.w(TAG, "getUserProfileDetails for " + email + ": cursor is null");
return cursor;
public boolean updateUserProfileImageUri(String email, String localImageUri) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
19-20.SQLite Database create update delete profile.
values.put(COLUMN_LOCAL_IMAGE_URI, localImageUri);
String whereClause = COLUMN_EMAIL + " = ?";
String[] whereArgs = {email};
int rowsAffected = 0;
try {
rowsAffected = db.update(TABLE_USERS, values, whereClause, whereArgs);
} catch (Exception e) {
Log.e(TAG, "Error updating profile image URI for email: " + email, e);
return false;
return rowsAffected > 0;
public void putStringSetting(String key, String value) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(COLUMN_SETTING_KEY, key);
values.put(COLUMN_SETTING_VALUE, value);
db.replace(TABLE_SETTINGS, null, values);
public String getStringSetting(String key, String defaultValue) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = null;
try {
19-20.SQLite Database create update delete profile.
cursor = db.query(TABLE_SETTINGS, new String[]{COLUMN_SETTING_VALUE},
COLUMN_SETTING_KEY + " = ?", new String[]{key}, null, null, null);
if (cursor != null && cursor.moveToFirst()) {
int valueIndex = cursor.getColumnIndex(COLUMN_SETTING_VALUE);
if (valueIndex != -1) {
return cursor.getString(valueIndex);
} finally {
if (cursor != null) cursor.close();
return defaultValue;
public void removeSetting(String key) {
SQLiteDatabase db = this.getWritableDatabase();
db.delete(TABLE_SETTINGS, COLUMN_SETTING_KEY + " = ?", new String[]{key});
Log.d(TAG, "Removed setting with key: " + key);
public void putBooleanSetting(String key, boolean value) {
putStringSetting(key, String.valueOf(value));
public boolean getBooleanSetting(String key, boolean defaultValue) {
String value = getStringSetting(key, String.valueOf(defaultValue));
try {
return Boolean.parseBoolean(value);
19-20.SQLite Database create update delete profile.
} catch (Exception e) {
return defaultValue;