# Getting file not a database exception when trying to attach database

**URL:** <https://discuss.zetetic.net/t/getting-file-not-a-database-exception-when-trying-to-attach-database/5742>\
**Category:** Issues\
**Created:** [January 30, 2023, 10:01am UTC](https://discuss.zetetic.net/t/getting-file-not-a-database-exception-when-trying-to-attach-database/5742 "2023-01-30T10:01:41Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![jbp](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.zetetic.net/jbp/32/1937_2.png) [@jbp](https://discuss.zetetic.net/u/jbp)\
**Post date:** [January 30, 2023, 10:01am UTC](https://discuss.zetetic.net/t/getting-file-not-a-database-exception-when-trying-to-attach-database/5742/1 "2023-01-30T10:01:41Z")

</div>

```auto
try {
            getWritableDatabase(mPassword)
        } catch (e: SQLiteException) {
            encrypt(mPassword.toByteArray())
            getWritableDatabase(mPassword)
        }

    private fun encrypt(passphrase: ByteArray) {
        val originalFile = context.getDatabasePath(DB_NAME)
        if (originalFile.exists()) {
            val newFilePath = originalFile.parent
            val newFile = File(newFilePath, "sqlcipherutils.db")

            // get database version from existing database
            val databaseVersion = SQLiteDatabase.openDatabase(
                originalFile.absolutePath,
                "",
                null,
                SQLiteDatabase.OPEN_READWRITE
            ).use { database ->
                database.version
            }

            SQLiteDatabase.openOrCreateDatabase(
                newFile.absolutePath, passphrase, null
            ).use {
                it.rawExecSQL( **

> //line 164

**
                    "ATTACH DATABASE '${originalFile.absolutePath}' AS sqlcipher4 KEY '${
                        String(
                            mPassword
                        )
                    }'"
                )
                it.rawExecSQL("SELECT sqlcipher_export('main', 'sqlcipher4')")
                it.rawExecSQL("DETACH DATABASE sqlcipher4")
                it.version = databaseVersion
            }
            originalFile.delete()
            newFile.renameTo(originalFile)
        } else {
            throw FileNotFoundException(originalFile.absolutePath.toString() + " not found")
        }
    }

```

Caused by: net.sqlcipher.database.SQLiteException: file is not a database  
at net.sqlcipher.database.SQLiteDatabase.native\_rawExecSQL(Native Method)  
at net.sqlcipher.database.SQLiteDatabase.rawExecSQL(SQLiteDatabase.java:2455)  
at .database.utils.DbHelper.encrypt(DbHelper.kt:164)

---

<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:** [January 30, 2023, 2:17pm UTC](https://discuss.zetetic.net/t/getting-file-not-a-database-exception-when-trying-to-attach-database/5742/2 "2023-01-30T14:17:57Z")

</div>

Hi @jbp,

It appears from your code that `originalFile` already exists and is not encrypted when you query for the version. You are then trying to open a database connection to `newFile`, and attaching `originalFile` but providing a key to `originalFile` which will not work as it is not encrypted. You may want to review the documentation on `cipher_migrate` \[1\], the convenience function which will allow you to encrypt a plaintext SQLite database among other operations.

* * *

1. [SQLCipher API - Zetetic](https://www.zetetic.net/sqlcipher/sqlcipher-api/#cipher_migrate)
