Troubleshooting SQLite Database Not Found in Android Project

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.

New contributor

Tamir nab is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật