# PRAGMA cipher\_migrate problem

**URL:** <https://discuss.zetetic.net/t/pragma-cipher-migrate-problem/3919>\
**Category:** Issues\
**Created:** [September 13, 2019, 5:14pm UTC](https://discuss.zetetic.net/t/pragma-cipher-migrate-problem/3919 "2019-09-13T17:14:12Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Matt\_Dupree](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.zetetic.net/matt_dupree/32/1147_2.png) [@Matt\_Dupree](https://discuss.zetetic.net/u/Matt_Dupree)\
**Post date:** [September 13, 2019, 5:14pm UTC](https://discuss.zetetic.net/t/pragma-cipher-migrate-problem/3919/1 "2019-09-13T17:14:12Z")

</div>

PRAGMA cipher\_migrate fails for Android Pie only. I see that the return value is 1 (inspected via cursor.getSTring(0), but I don’t know how to get additional information as to why the error is occurring.

How can I do that? Is the only way to run the pragma from the CLI using the community command line shell?

---

<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:** [September 16, 2019, 1:55pm UTC](https://discuss.zetetic.net/t/pragma-cipher-migrate-problem/3919/2 "2019-09-16T13:55:50Z")

</div>

Hello @Matt_Dupree

I’m sorry to hear you are experiencing an issue with migration. A couple of follow-up questions:

- What version of SQLCipher was used to create the database you are attempting to migrate?
- Did you use any non-default runtime settings when creating the database you are attempting to migrate?

You can also run the same `pragma cipher_migrate` using the command line shell.

---

<div class="post-metadata">

**Author:** ![Matt\_Dupree](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.zetetic.net/matt_dupree/32/1147_2.png) [@Matt\_Dupree](https://discuss.zetetic.net/u/Matt_Dupree)\
**Post date:** [October 1, 2019, 2:18pm UTC](https://discuss.zetetic.net/t/pragma-cipher-migrate-problem/3919/3 "2019-10-01T14:18:49Z")

</div>

Appreciate the response. Turns out the migration was failing because we had updated our target Android SDK version. We figured out why and have implemented a work around. 🙂

---

<div class="post-metadata">

**Author:** ![sjlombardo](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.zetetic.net/sjlombardo/32/3_2.png) [@sjlombardo](https://discuss.zetetic.net/u/sjlombardo)\
**Post date:** [October 1, 2019, 7:20pm UTC](https://discuss.zetetic.net/t/pragma-cipher-migrate-problem/3919/4 "2019-10-01T19:20:59Z")

</div>

> [@Matt\_Dupree](#):
>
> we had updated our target Android SDK version

Hi @Matt_Dupree - thanks for getting back to us to let us know this is resolved. Could you explain a bit more how the target SDK change impacted the migration? It would be good to know the details in case anyone runs into this in the future. Thanks!

---

<div class="post-metadata">

**Author:** ![Matt\_Dupree](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.zetetic.net/matt_dupree/32/1147_2.png) [@Matt\_Dupree](https://discuss.zetetic.net/u/Matt_Dupree)\
**Post date:** [October 1, 2019, 9:57pm UTC](https://discuss.zetetic.net/t/pragma-cipher-migrate-problem/3919/5 "2019-10-01T21:57:37Z")

</div>

Our situation was pretty unique. We were using Build.SERIAL as a _part_ of the key to access the database and the behavior of Build.SERIAL changed when we updated the target SDK version

---

<div class="post-metadata">

**Author:** ![kemeyo4490](https://avatars.discourse-cdn.com/v4/letter/k/d07c76/32.png) [@kemeyo4490](https://discuss.zetetic.net/u/kemeyo4490)\
**Post date:** [February 25, 2020, 9:05pm UTC](https://discuss.zetetic.net/t/pragma-cipher-migrate-problem/3919/6 "2020-02-25T21:05:22Z")

</div>

Hi everyone!  
I’ve studied couple of threads related to migration issues on Android similar to this one [Upgrading to SQLCipher 4](https://discuss.zetetic.net/t/upgrading-to-sqlcipher-4/3283/43) but still facing some problems.  
The app is using SqlCipher for Android v3.5.7 together with OrmLite. I need to switch to Room Persistence Library but to do that I need to update version of SqlCipher to latest one since it’s compatible with Room.

So, current implementation (with v3.5.7) has custom settings described in the hook’s postkey

```
database.rawExecSQL("PRAGMA cipher_page_size = 2048;");
database.rawExecSQL("PRAGMA cipher = 'aes-128-cfb';");
database.rawExecSQL("PRAGMA kdf_iter = 20000;");

```

As was suggested the proper way to do it is custom export migration. So, after those three line from the hook I’m calling this function to migrate my db.

```
fun migrateToCipher4(context: Context, database: SQLiteDatabase) {
    val newDbName = "<my-new-db-name>"
    val newDbFile = context.getDatabasePath("$newDbName.db")
    val newDbPath = newDbFile.absolutePath
    val newDbKey = "<my-new-db-password>"
    val newDbVersion = 1

    val dbHelper = DatabaseHelper4(context, "$newDbName.db", null, newDbVersion, newDbKey)
    val db = dbHelper.getWritableDatabase(newDbKey)
    db.close()
    dbHelper.close()
    
    database.rawExecSQL("ATTACH DATABASE '$newDbPath' AS $newDbName KEY '$newDbKey';")
    database.rawExecSQL("SELECT sqlcipher_export('$newDbName');")
    database.rawExecSQL("DETACH DATABASE $newDbName;")
}

```

But it fails during execution of ATTACH query with ‘file is not a database’ error.  
net.sqlcipher.database.SQLiteException: file is not a database  
at net.sqlcipher.database.SQLiteDatabase.native\_rawExecSQL(Native Method)

Besides, new db file is created and I can open it with DB browser for SQLite with default SqlCipher4 settings. But it’s empty since error happens before export query runs.

Any ideas what could’ve been missing? Thanks in advance.

---

<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:** [February 25, 2020, 9:26pm UTC](https://discuss.zetetic.net/t/pragma-cipher-migrate-problem/3919/7 "2020-02-25T21:26:16Z")

</div>

Hi @kemeyo4490

As part of the [SQLCipher 4.0.0](https://www.zetetic.net/blog/2018/11/30/sqlcipher-400-release/) release, the `PRAGMA cipher` command is now disabled and no longer supported (after multi-year deprecation). So you will likely need to convert your databases using 3.5.7 to use the standard cipher (AES 256 in CBC mode) before you are able to upgrade, or create a custom build of SQLCipher for Android to support your needs.

---

<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:** [October 7, 2020, 1:18pm UTC](https://discuss.zetetic.net/t/pragma-cipher-migrate-problem/3919/8 "2020-10-07T13:18:44Z")

</div>

4 posts were merged into an existing topic: [SQLCipher(v) compatibility with openssl(v)](https://discuss.zetetic.net/t/sqlcipher-v-compatibility-with-openssl-v/4594/9)
