我想在Android Room中使用预填充的数据库。我找到了一种通过使用回调来实现此目标的方法,并填充了数据库文件。 但是出了点问题,我确定数据库可以正常复制,但是在设备监视器和android模拟器中仍然为空。你能帮我吗
public abstract class AppDatabase extends RoomDatabase {
private static AppDatabase INSTANCE;
private static final String DB_NAME = "base.db";
static Context ctx;
public abstract Dao dao();
public static AppDatabase getDatabase(Context context) {
if (INSTANCE == null) {
ctx = context;
synchronized (AppDatabase.class) {
if (INSTANCE == null) {
INSTANCE = Room.databaseBuilder(context,
AppDatabase.class, DB_NAME)
.allowMainThreadQueries()
.addCallback(rdc)
.build();
}
}
}
return INSTANCE;
}
private static RoomDatabase.Callback rdc = new RoomDatabase.Callback() {
public void onCreate(SupportSQLiteDatabase db) {
new PopulateDbAsync(INSTANCE, ctx).execute();
Log.d("db create ", "table created when db created first time in onCreate");
}
public void onOpen(@NonNull SupportSQLiteDatabase db) {
ContentValues contentValues = new ContentValues();
}
};
private static class PopulateDbAsync extends AsyncTask<Void, Void, Void> {
private Dao dao;
AssetManager assetManager = ctx.getAssets();
PopulateDbAsync(AppDatabase db, Context context) {
Dao = db.Dao();
ctx = context;
}
@Override
protected Void doInBackground(final Void... params) {
String DB_PATH = "/data/data/mypackage/databases/";
String DB_NAME = "base.db";
try {
Log.d("AppDatabase","Trying copy database file");
OutputStream myOutput = new FileOutputStream(DB_PATH + DB_NAME);
byte[] buffer = new byte[1024];
int length;
InputStream myInput = ctx.getAssets().open("base.db");
while ((length = myInput.read(buffer)) > 0) {
myOutput.write(buffer, 0, length);
}
myInput.close();
myOutput.flush();
myOutput.close();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
}
答案 0 :(得分:3)
花了6个小时进行研究和研发后,我才解决了。
上下文是这样的:-我要将已经存在的finaldb.db(存在于assests文件夹中)放入会议室数据库。
第1步: 从这里link
复制此框架文件第2步: 您需要迁移,寒意我有代码:)
@Database(entities = {Status.class}, version = 1,exportSchema = false)
public abstract class AppDatabase extends RoomDatabase {
public abstract DataDao StatusDao();
private static AppDatabase INSTANCE;
public static AppDatabase getDatabase(Context context) {
if (INSTANCE == null) {
INSTANCE = createDatabase(context);
}
return (INSTANCE);
}
private static final Migration MIGRATION_2_3 = new Migration(1, 2) {
@Override
public void migrate(@NonNull SupportSQLiteDatabase database) {
Log.d("kkkk","bc");
String SQL_CREATE_TABLE = "CREATE TABLE IF NOT EXISTS 'Status' " +
"( 'id' INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT," +
" 'category' TEXT NOT NULL," +
" 'sub_category' TEXT NOT NULL," +
" 'content' TEXT NOT NULL," +
" 'favourite' INTEGER DEFAULT(0))";
database.execSQL(SQL_CREATE_TABLE);
}
};
private static AppDatabase createDatabase(Context context) {
RoomDatabase.Builder<AppDatabase> builder =
Room.databaseBuilder(context.getApplicationContext(), AppDatabase.class,
context.getString(R.string.dbase_name));
return (builder.openHelperFactory(new AssetSQLiteOpenHelperFactory())
.allowMainThreadQueries()
.addMigrations(MIGRATION_2_3)
.build());
}
}
在MIGRATION_2_3中,您必须创建与当前数据库完全相同的表(该数据库位于assests文件夹中)
想了解migration
第3步: 现在已在会议室数据库中成功创建表!
万一发生崩溃,请参阅您的logcat,其中以易于理解的形式编写。
答案 1 :(得分:1)
您不能正确地使用onCreate
方法复制数据库。
调用onCreate
方法时,数据库已创建(创建的数据库传递到该方法)。您必须在onCreate
方法之前和数据库打开之前进行复制。
您可以覆盖RoomDatabase的 init
方法,并从该方法进行复制,或者在调用databaseBuilder之前进行复制。
答案 2 :(得分:1)
我解决了。 数据库类:
@Database(entities = {Entity1.class, Entity2.class, Entity3.class}, version = 1, exportSchema = false)
public abstract class AppDatabase extends RoomDatabase {
private static AppDatabase INSTANCE;
public abstract Entity1Dao 1Dao();
public abstract Entity2Dao 2Dao();
public abstract Entity3Dao 3Dao();
public static AppDatabase getDatabase(Context context) {
if (INSTANCE == null) {
INSTANCE = createDatabase(context);
}
return (INSTANCE);
}
private static AppDatabase createDatabase(Context context) {
RoomDatabase.Builder<AppDatabase> builder =
Room.databaseBuilder(context.getApplicationContext(), AppDatabase.class,
context.getString(R.string.dbase_name));
return (builder.openHelperFactory(new AssetSQLiteOpenHelperFactory())
.allowMainThreadQueries()
.build());
}
}
您还应该获得SQL Helpers,link
答案 3 :(得分:0)
我的问题与 OP 有点不同。
我是从内部存储中复制数据库文件,该文件是从 Internet 下载的,而不是从资产中。并且 java.lang.RuntimeException: Unable to copy database file
被提出是因为我在此之前没有授予 READ_EXTERNAL_STORAGE
,或者一般授予 WRITE_EXTERNAL_STORAGE
作为 READ_EXTERNAL_STORAGE
包含在 WRITE_EXTERNAL_STORAGE
中并且已经需要写入下载文件的权限。