How do I locate the SQLIte database on Android

I tried the following code but it does not appear to work.

    public DataBase(Context context) {
        super(context, getDatabasePath(context), null, DATABASE_VERSION);
        this.context = context;
    }

    private static String getDatabasePath(Context context) {
        // Obtenir le chemin du dossier "Documents"
        File documentsDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS);
        // Créer le dossier "Documents" s'il n'existe pas
        if (!documentsDir.exists()) {
            documentsDir.mkdirs();
        }
        // Retourner le chemin complet de la base de données
        return new File(documentsDir, DATABASE_NAME).getAbsolutePath();
    }
`
```

New contributor

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

You’re trying to store the SQLite database in the public Documents directory, which isn’t the recommended approach for Android apps.

Here’s a proper way to do it:

public class DatabaseHelper extends SQLiteOpenHelper {
    private static final String DATABASE_NAME = "your_database.db";
    private static final int DATABASE_VERSION = 1;
    private final Context context;

    public DatabaseHelper(Context context) {
        // Use the app's private database directory
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        this.context = context;
    }

    // Get the database file path (for debugging purposes)
    public String getDatabasePath() {
        return context.getDatabasePath(DATABASE_NAME).getAbsolutePath();
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // Create your tables here
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // Handle database upgrades here
    }

    // Optional: Method to check if database exists
    public boolean databaseExists() {
        File dbFile = context.getDatabasePath(DATABASE_NAME);
        return dbFile.exists();
    }

    // Optional: Method to get database size
    public long getDatabaseSize() {
        File dbFile = context.getDatabasePath(DATABASE_NAME);
        return dbFile.length();
    }
}

What you need to know about SQLite database location in Android:

Default Location:

  • Android stores app databases in the private app directory: /data/data/your.package.name/databases/
  • You don’t need to specify this path manually; Android handles it

To use the database:

DatabaseHelper dbHelper = new DatabaseHelper(context);
SQLiteDatabase db = dbHelper.getWritableDatabase();

To find the actual path (for debugging):

String path = dbHelper.getDatabasePath();
Log.d("Database Path", path);

Note the following:

  • Don’t store databases in public directories like Documents
  • Use the app’s private storage for security
  • Let Android manage the database location through Context.getDatabasePath()
  • The database is automatically created in the correct location when you first call getWritableDatabase()

1

You should a) ensure that a valid Context is passed when calling the getDatabasePath method and importantly, if using the default Android SQLite database location, b) use the Context‘s getDatabasePath method.

e.g.

File documentsDir = context.getDatabasePath("whatever_as_it_does_not_matter_as_dir_will_be_the_same").parentFile
  • Note the file name as it is descriptive (i.e. the name, if wanting JUST the directory (the parent), is irrelevant (as long as it is valid)).

    • note that you would very likely use DATABASE_NAME, as in context.getDatabasePath(DATABASE_NAME).parentFile
  • Although the link to getDatabasePath mentions openOrCreateDatabase…. this method will be invoked by other convenience API class/methods such as the SQLiteOpenHelper class.

    • It very much appears that your Database class extends the SQliteOpenHelper class.
  • You may wish to refer to https://developer.android.com/training/data-storage. However, note that this tries to push one towards utilising Android Room and thus hides the fact that Room utilises the underlying SQLite API, which is obviously perfectly valid to utilise without resorting to utilising Room.

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