There is a method taken from https://metanit.com/java/android/14.4.php:
public SQLiteDatabase open()throws SQLException {
return SQLiteDatabase.openDatabase(DB_PATH, null, SQLiteDatabase.OPEN_READWRITE);
}
The problem is that in this example, the database is created explicitly in the assets folder
But in my case, the database is created in the class:
public class PreparationDBHelper extends SQLiteOpenHelper {
public static final int DATABASE_VERSION = 1;
public static final String DATABASE_NAME = "PreparationDb";
public static final String TABLE_TEXT = "textTable";
public static final String KEY_ID = "_id";
public static final String KEY_NAME = "name";
public static final String KEY_TEXT = "text";
public PreparationDBHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table " + TABLE_TEXT + "(" + KEY_ID
+ " INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL," + KEY_NAME + " text," + KEY_TEXT + " text" + ")");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("drop table if exists " + TABLE_TEXT);
onCreate(db);
}
/* public SQLiteDatabase open()throws SQLException {
return SQLiteDatabase.openDatabase(DB_PATH, null, SQLiteDatabase.OPEN_READWRITE);
}*/
}
How do I specify the path to the database in the open () method in my case?
How do I specify the path to the database in the open () method in my case?
You don't need to have an open()
method and probably shouldn't. Rather from an instance of the PreparationDBHelper
class, you use either the getWritableDatabase()
or the getReadableDatabase()
method to get an opened SQLiteDatabase.
getReabaleDatabase
will in most situations return a writable database, it DOES NOT protect the database against being updated.
PreparationDBHelper
extends SQliteOpenHelper
so named as it helps you to open the database. Hence there is little need to otherwise open the database.
get????ableDatabase
methods is called.If you really want the path then in your case you can use your_context.getDatabasePath(DATABASE_NAME)
(your_context being a Context) which returns a File object from which you can retrieve the path as a string e.g. String myDBPath = your_context.getDatabasePath(DATABASE_NAME).getPath()
(see File as there are also getAbsolutePath
and getCanonicalPath
methods).
getWritableDatabase
and getReadableDatabase
) getPath() method.In the example code you have pointed to DB_PATH =context.getFilesDir().getPath() + DB_NAME;
is used (this will place the database at data/data/<package_name>/ rather than at the recommended data/data/<package_name>/databases/). Really the example should have used DB_PATH = context.getDatabasePath(DB_NAME).getPath();
The problem is that in this example, the database is created explicitly in the assets folder.
It is not, the database is copied (if the database doesn't already exist) from the assets folder to it's final location (which is not the recommended location).
file.mkdirs()
.