I am trying to Pre-Populate my Android ROOM with a database2.db file which I copied over from my device by making the app generate the Database Values pragmatically. Which worked when I initially Installed the app however upon Uninstalling and Reinstalling to force a ROOM database build I get the following Error
E/AndroidRuntime: FATAL EXCEPTION: arch_disk_io_0
Process: com.Adictya.timely, PID: 16244
java.lang.RuntimeException: Exception while computing database live data.
at androidx.room.RoomTrackingLiveData$1.run(RoomTrackingLiveData.java:92)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at java.lang.Thread.run(Thread.java:919)
Caused by: java.lang.RuntimeException: Unable to copy database file.
at androidx.room.SQLiteCopyOpenHelper.verifyDatabaseFile(SQLiteCopyOpenHelper.java:131)
.
.
.
.
Caused by: java.io.FileNotFoundException: database/database2.db
at com.Adictya.timely.data.TimeSlotsDAO_Impl$7.call(TimeSlotsDAO_Impl.java:313)
.
.
.
The App works fine once I remove the .createFromAsset("database/database2.db
)` and I have ensured the file is in assets/database package.
Here is my RoomDatabase Class
@Database(entities = {Classes.class, TimeSlots.class}, version = 1, exportSchema = false)
public abstract class TimeSlotsRoomDatabase extends RoomDatabase {
public static volatile TimeSlotsRoomDatabase INSTANCE;
public abstract ClassesDAO classesDAO();
public abstract TimeSlotsDAO timeSlotsDAO();
public static TimeSlotsRoomDatabase getDatabase(final Context context){
if (INSTANCE == null){
synchronized (TimeSlotsRoomDatabase.class){
if (INSTANCE == null){
//create our db
INSTANCE = Room.databaseBuilder(context.getApplicationContext(),TimeSlotsRoomDatabase.class,"timeslots_db")
.addCallback(roomDatabaseCallback).createFromAsset("database/database2.db").fallbackToDestructiveMigration().build();
// INSTANCE = Room.databaseBuilder(context.getApplicationContext(),TimeSlotsRoomDatabase.class,"timeslots_db")
// .addCallback(roomDatabaseCallback).build();
}
}
}
return INSTANCE;
}
public static RoomDatabase.Callback roomDatabaseCallback = new RoomDatabase.Callback() {
@Override
public void onOpen(@NonNull SupportSQLiteDatabase db){
super.onOpen(db);
new PopulateDbAsync(INSTANCE).execute();
}
};
private static class PopulateDbAsync extends AsyncTask<Void, Void, Void>{
private final TimeSlotsDAO timeSlotsDAO;
private final ClassesDAO classesDAO;
public PopulateDbAsync(TimeSlotsRoomDatabase db){
timeSlotsDAO = db.timeSlotsDAO();
classesDAO = db.classesDAO();
}
@Override
protected Void doInBackground(Void... voids) {
timeSlotsDAO.deleteAll();
classesDAO.deleteAll();
//for testing
Classes classes = new Classes("CSE1002","Microprocessor and Interfacing","Micro");
classes = new Classes("CSE1003","Internet of Things","IOT");
TimeSlots timeSlots = new TimeSlots("A1",0,0,"8:00 AM","Microprocessor and Interfacing","SJT-522");
timeSlotsDAO.insert(timeSlots);
}
}
}
You have a small mistake, replace .createFromAsset("database/database2.db)
to .createFromAsset("databases/database2.db)
. Database package would be databases/
, not enter code here
I made a silly mistake and place the assets folder inside my main/java directory. It should be as CommonsWare pointed out
as a sub-directory of src/, making it a peer of java/, res/, etc.
Thanks again for your help.
您应该将您的文件放在app/src/main/assets/databases/database2.db
目录中并像这样处理它.createFromAsset("databases/database2.db")
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.