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();
}
`
```
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 incontext.getDatabasePath(DATABASE_NAME).parentFile
- note that you would very likely use
-
Although the link to
getDatabasePath
mentionsopenOrCreateDatabase
…. this method will be invoked by other convenience API class/methods such as theSQLiteOpenHelper
class.- It very much appears that your
Database
class extends theSQliteOpenHelper
class.
- It very much appears that your
-
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.