gpt4 book ai didi

尝试预填充 ROOM 数据库时出现 java.lang.RuntimeException : Unable to copy database file.

转载 作者:行者123 更新时间:2023-12-04 23:56:32 26 4
gpt4 key购买 nike

我正在尝试使用我从设备复制的 database2.db 文件预填充我的 Android ROOM,方法是让应用程序实用地生成数据库值。这在我最初安装应用程序时有效,但是在卸载和重新安装以强制构建 ROOM 数据库时,我收到以下错误

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) 
.
.
.

一旦我删除了 .createFromAsset("database/database2.db)` 并且我确保该文件位于 assets/database 包中,应用程序就可以正常工作。

这是我的 RoomDatabase 类

@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);

}
}
}

最佳答案

我犯了一个愚蠢的错误,将 Assets 文件夹放在我的 main/java 目录中。它应该是 CommonsWare指出

as a sub-directory of src/, making it a peer of java/, res/, etc.

再次感谢您的帮助。

关于尝试预填充 ROOM 数据库时出现 java.lang.RuntimeException : Unable to copy database file.,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60905190/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com