如何解决房间数据库关闭后如何重新打开
我想加密房间的现有数据库。 为此,我使用 createFromAsset() 方法创建了数据库并将其关闭。 加密后再次尝试重建数据库时, 数据库内容为空。
String str = "pass";
char[] pass = str.tochararray();
//build
AppDatabase db = Room.databaseBuilder(getApplicationContext(),AppDatabase.class,"dictionary_word32")
.createFromAsset("database/dictionary_word14")
.addMigrations(MIGRATION_1_2)
.addMigrations(MIGRATION_3_4)
.addMigrations(MIGRATION_4_5)
.addMigrations(MIGRATION_5_6)
.allowMainThreadQueries()
.build();
db.close();
//encryption
sqlCipherUtils.State state = sqlCipherUtils.getDatabaseState(getApplicationContext(),"dictionary_word31");
Log.d("TAG",String.valueOf(state));
if (state == sqlCipherUtils.State.UNENCRYPTED) {
try {
sqlCipherUtils.encrypt(getApplicationContext(),"dictionary_word31",pass);
Log.d("TAG","Successfully encrypted database!");
} catch (IOException e) {
Log.e("TAG","Failed to encrypt prevIoUsly unencrypted database!");
e.printstacktrace();
}
}
final byte[] passphrase = sqliteDatabase.getBytes(pass);
final SupportFactory factory = new SupportFactory(passphrase);
//reopen
db = Room.databaseBuilder(getApplicationContext(),"dictionary_word32")
.allowMainThreadQueries()
.openHelperFactory(factory)
.build();
即使省略加密,结果也一样。
//build
AppDatabase db = Room.databaseBuilder(getApplicationContext(),"dictionary_word32")
.createFromAsset("database/dictionary_word14")
.addMigrations(MIGRATION_1_2)
.addMigrations(MIGRATION_3_4)
.addMigrations(MIGRATION_4_5)
.addMigrations(MIGRATION_5_6)
.allowMainThreadQueries()
.build();
db.close();
//reopen
db = Room.databaseBuilder(getApplicationContext(),"dictionary_word32")
.allowMainThreadQueries()
.build();
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。