Encrypting existing database

I’m a beginner. I read this topic(Encrypting database with Room createFromAsset() method - #2 by commonsguy) and tried to imitate it, but it doesn’t work.
The log says database is encrypted. But when I look at the contents of the database by Database Inspecter,it’s empty.Thank in advance for any help.

String str = "pass";
char[] pass = str.toCharArray();

Room.databaseBuilder(getApplicationContext(), AppDatabase.class, "dictionary_word16")
        .createFromAsset("database/dictionary_word14")
        .addMigrations(MIGRATION_1_2)
        .addMigrations(MIGRATION_3_4)
        .addMigrations(MIGRATION_4_5)
        .addMigrations(MIGRATION_5_6)
        .allowMainThreadQueries()
        .build();

SQLCipherUtils.State state = SQLCipherUtils.getDatabaseState(getApplicationContext(), "dictionary_word16");
Log.d("TAG", String.valueOf(state));
if (state == SQLCipherUtils.State.UNENCRYPTED) {
    try {
        SQLCipherUtils.encrypt(getApplicationContext(), "dictionary_word16", 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);

AppDatabase db = Room.databaseBuilder(getApplicationContext(), AppDatabase.class, "dictionary_word16")
        .allowMainThreadQueries()
        .openHelperFactory(factory)
        .build();

A few notes:

  1. I don’t know that the database actually exists after your initial build(). I think that it might be lazy-created on first use.

  2. You should close() the initial database before trying to encrypt it.

  3. While I have not tried Database Inspector with SQLCipher for Android, I would be surprised if it is supported. Did it prompt you for a passphrase?

Thank you very much for kind reply. I’m sorry that I couldn’t get back to you sooner.

1.I don’t know that the database actually exists after your initial build(). I think that it might be lazy-created on first use.
When there is the first build, I could look at contents of the database.
Room.databaseBuilder(getApplicationContext(), AppDatabase.class, “dictionary_word16”)
.createFromAsset(“database/dictionary_word14”)
.addMigrations(MIGRATION_1_2)
.addMigrations(MIGRATION_3_4)
.addMigrations(MIGRATION_4_5)
.addMigrations(MIGRATION_5_6)
.allowMainThreadQueries()
.build();

When I made code without encryption (build → close → build), the contents of the database were empty.
//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();

    //reopen
    db = Room.databaseBuilder(getApplicationContext(), AppDatabase.class, "dictionary_word32")
            .allowMainThreadQueries()
            .build();

The second build doesn’t seem to work.

2.You should close() the initial database before trying to encrypt it.

I added close ().

3.While I have not tried Database Inspector with SQLCipher for Android,

My apologies,I misunderstood that it was supported.