# Fail to open database after encryption in Android

**URL:** <https://discuss.zetetic.net/t/fail-to-open-database-after-encryption-in-android/2730>\
**Category:** SQLCipher\
**Created:** [March 22, 2018, 3:20am UTC](https://discuss.zetetic.net/t/fail-to-open-database-after-encryption-in-android/2730 "2018-03-22T03:20:22Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![min0623](https://avatars.discourse-cdn.com/v4/letter/m/48db29/32.png) [@min0623](https://discuss.zetetic.net/u/min0623)\
**Post date:** [March 22, 2018, 3:20am UTC](https://discuss.zetetic.net/t/fail-to-open-database-after-encryption-in-android/2730/1 "2018-03-22T03:20:22Z")

</div>

Hi, I’m a bit worried this may be duplicate issue, but not any answer helped to resolve my issue, so I would like to share my code to get some idea what’s going wrong.

```
private static synchronized void createSession(Context context) {
    try {
        SQLiteDatabase.loadLibs(context);
        encrypt(context, DBOpenHelper.DB_NAME, "password");
    } catch (IOException e) {
        e.printStackTrace();
    }
    DBOpenHelper helper = new DBOpenHelper(context);
    db = helper.getEncryptedWritableDb("password");
}

private static void encrypt(Context context, String dbName, String passphrase) throws IOException {
    File originalFile = context.getDatabasePath(dbName);
    File encryptedFile = context.getDatabasePath(ENCRYPTED_DB_NAME);

    if (originalFile.exists() && !encryptedFile.exists()) {
        encryptedFile.mkdir();
        SQLiteDatabase.openOrCreateDatabase(encryptedFile, passphrase, null);

        SQLiteDatabase db = SQLiteDatabase.openDatabase(originalFile.getAbsolutePath(), "", null, SQLiteDatabase.OPEN_READWRITE);

        db.rawExecSQL(String.format("ATTACH DATABASE '%s' AS encrypted KEY '%s';",
                encryptedFile.getAbsolutePath(), passphrase));

        db.rawExecSQL("SELECT sqlcipher_export('encrypted')");
        db.rawExecSQL("DETACH DATABASE encrypted;");

        int version = db.getVersion();

        db.close();

        db = SQLiteDatabase.openDatabase(encryptedFile.getAbsolutePath(), passphrase, null, SQLiteDatabase.OPEN_READWRITE);

        db.setVersion(version);
        db.close();

        originalFile.delete();
        encryptedFile.renameTo(originalFile);
    }
}

```

This is my code to encrypt original local unencrypted databases whenever the application is updated. However, I’m getting error when I try to open the encrypted database even though I’m using the same passphrase(in this case, “password”). Following is the error I’m receiving.

```
03-22 11:37:17.599 9080-9080/com.example E/SQLiteOpenHelper: Couldn't open %my-db-name% for writing (will try read-only):
                                                               net.sqlcipher.database.SQLiteException: file is not a database: , while compiling: select count(*) from sqlite_master;
                                                                   at net.sqlcipher.database.SQLiteCompiledSql.native_compile(Native Method)
                                                                   at net.sqlcipher.database.SQLiteCompiledSql.compile(SQLiteCompiledSql.java:91)
                                                                   at net.sqlcipher.database.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:64)
                                                                   at net.sqlcipher.database.SQLiteProgram.<init>(SQLiteProgram.java:89)
                                                                   at net.sqlcipher.database.SQLiteQuery.<init>(SQLiteQuery.java:48)
                                                                   at net.sqlcipher.database.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:60)
                                                                   at net.sqlcipher.database.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1867)
                                                                   at net.sqlcipher.database.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1785)
                                                                   at net.sqlcipher.database.SQLiteDatabase.keyDatabase(SQLiteDatabase.java:2486)
                                                                   at net.sqlcipher.database.SQLiteDatabase.openDatabaseInternal(SQLiteDatabase.java:2415)
                                                                   at net.sqlcipher.database.SQLiteDatabase.openDatabase(SQLiteDatabase.java:1149)
                                                                   at net.sqlcipher.database.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:1212)
                                                                   at net.sqlcipher.database.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:162)
                                                                   at net.sqlcipher.database.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:227)
                                                                   at net.sqlcipher.database.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:214)
                                                                   at org.greenrobot.greendao.database.DatabaseOpenHelper.getEncryptedWritableDb(DatabaseOpenHelper.java:134)

```

There is no problem when I pass empty string when I open my database: it just opens unencrypted database.  
How can I open the encrypted database with my passphrase?

---

<div class="post-metadata">

**Author:** ![developernotes](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.zetetic.net/developernotes/32/1309_2.png) [@developernotes](https://discuss.zetetic.net/u/developernotes)\
**Post date:** [March 22, 2018, 1:45pm UTC](https://discuss.zetetic.net/t/fail-to-open-database-after-encryption-in-android/2730/2 "2018-03-22T13:45:07Z")

</div>

Hi @min0623

Try removing this line from your example:

```auto
SQLiteDatabase.openOrCreateDatabase(encryptedFile, passphrase, null);

```

The `ATTACH` statement will create the file if it doesn’t already exist. We have an example of importing a plain-text database, encrypting it [here](https://github.com/sqlcipher/sqlcipher-android-tests/blob/master/app/src/main/java/net/zetetic/tests/ImportUnencryptedDatabaseTest.java).

---

<div class="post-metadata">

**Author:** ![min0623](https://avatars.discourse-cdn.com/v4/letter/m/48db29/32.png) [@min0623](https://discuss.zetetic.net/u/min0623)\
**Post date:** [March 23, 2018, 1:43am UTC](https://discuss.zetetic.net/t/fail-to-open-database-after-encryption-in-android/2730/3 "2018-03-23T01:43:20Z")

</div>

Thanks @developernotes

Removing the line throws another SQLite exception on the first `rawExecSQL`.

```
Caused by: net.sqlcipher.database.SQLiteException: unable to open database: /data/user/0/my.package/databases/db-encrypted
                                                                    at net.sqlcipher.database.SQLiteDatabase.native_rawExecSQL(Native Method)
                                                                    at net.sqlcipher.database.SQLiteDatabase.rawExecSQL(SQLiteDatabase.java:2281) 

```

This is another error that occured.

```
E/Database: close() was never explicitly called on database '/data/user/0/my.package/databases/remember-db' 
                                                       net.sqlcipher.database.DatabaseObjectNotClosedException: Application did not close the cursor or database object that was opened here
                                                           at net.sqlcipher.database.SQLiteDatabase.<init>(SQLiteDatabase.java:2402)
                                                           at net.sqlcipher.database.SQLiteDatabase.openDatabase(SQLiteDatabase.java:1148)
                                                           at net.sqlcipher.database.SQLiteDatabase.openDatabase(SQLiteDatabase.java:1116)
                                                           at net.sqlcipher.database.SQLiteDatabase.openDatabase(SQLiteDatabase.java:1065)
                                                           at net.sqlcipher.database.SQLiteDatabase.openDatabase(SQLiteDatabase.java:1019)

```

This was why I inserted that line in my code. Any idea what is happening?

---

<div class="post-metadata">

**Author:** ![developernotes](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.zetetic.net/developernotes/32/1309_2.png) [@developernotes](https://discuss.zetetic.net/u/developernotes)\
**Post date:** [March 23, 2018, 12:58pm UTC](https://discuss.zetetic.net/t/fail-to-open-database-after-encryption-in-android/2730/4 "2018-03-23T12:58:15Z")

</div>

Hi @min0623

What are your results of running the SQLCipher for Android [test suite](https://github.com/sqlcipher/sqlcipher-android-tests)?
