Android 不要使用robolectric重新创建数据库
我知道Robolectric并没有使用真正的SQLite数据库,而是使用内存数据库。但每次调用getWritableDatabase或getWritableDatabase时都会重新创建数据库Android 不要使用robolectric重新创建数据库,android,sqlite,android-sqlite,robolectric,android-testing,Android,Sqlite,Android Sqlite,Robolectric,Android Testing,我知道Robolectric并没有使用真正的SQLite数据库,而是使用内存数据库。但每次调用getWritableDatabase或getWritableDatabase时都会重新创建数据库 有没有办法让Robolectric重新创建数据库?比如说,只有在调用安装程序或类似的情况下?我从github下载了Robolectric源代码,并在回答这个问题时进行了更改: 在ShadowSQLiteOpenHelper类中,我做了以下更改: 定义此字段: private static Context
有没有办法让Robolectric重新创建数据库?比如说,只有在调用安装程序或类似的情况下?我从github下载了Robolectric源代码,并在回答这个问题时进行了更改: 在ShadowSQLiteOpenHelper类中,我做了以下更改: 定义此字段:
private static Context previousContext;
在中,构造函数添加了以下内容:
if (previousContext == null) {
previousContext = context;
} else {
if(previousContext == context) {
return;
} else {
previousContext = context;
}
}
更改了getDatabase方法,如下所示:
private SQLiteDatabase getDatabase() {
if (database == null) {
database = SQLiteDatabase.openDatabase("path", null, 0);
realHelper.onCreate(database);
}
realHelper.onOpen(database);
return database;
}
现在一切都很完美。我认为您的数据库位于asset文件夹中,当您调用getWritableDatabase时,每次都会将其复制到应用程序数据库文件夹,即/data/data//databases/。检查此链接@ZohraKhan我的资产文件夹中没有数据库。数据库是在我的助手中的onCreate中创建的,它扩展了SQLiteOpenHelper类。问题是Robolectric正在使用内存中的数据库,每次调用getWritableDatabase时都会重新创建该数据库。我想知道我怎样才能改变这个