# Migrating Android Application from SQLCipher 3 to 4

**URL:** <https://discuss.zetetic.net/t/migrating-android-application-from-sqlcipher-3-to-4/3943>\
**Category:** Uncategorized\
**Created:** [September 27, 2019, 10:09am UTC](https://discuss.zetetic.net/t/migrating-android-application-from-sqlcipher-3-to-4/3943 "2019-09-27T10:09:29Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![gupta126](https://avatars.discourse-cdn.com/v4/letter/g/22d042/32.png) [@gupta126](https://discuss.zetetic.net/u/gupta126)\
**Post date:** [September 27, 2019, 10:09am UTC](https://discuss.zetetic.net/t/migrating-android-application-from-sqlcipher-3-to-4/3943/1 "2019-09-27T10:09:29Z")

</div>

HI @developernotes,

I have checked options provided in [Update to version 4](https://discuss.zetetic.net/t/upgrading-to-sqlcipher-4/3283)  
We have released Production app with SQLCipher v3.5.7 now we are upgrading the library to 4.1.3  
so before onUpgrade method gets invoke it crashes  
can you please let me know how do we need to execute “PRAGMA cipher\_migrate”?

I have followed example given in link : [Example Link](https://discuss.zetetic.net/t/upgrading-sqlcipher-for-android-from-3-to-4/3580)

---

<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 27, 2019, 12:17pm UTC](https://discuss.zetetic.net/t/migrating-android-application-from-sqlcipher-3-to-4/3943/2 "2019-09-27T12:17:00Z")

</div>

Hi @gupta126

You can perform the `cipher_migrate` command via a `SQLiteDatabaseHook` within the `postKey` operation. An example from our test suite can be found [here](https://github.com/sqlcipher/sqlcipher-android-tests/blob/master/app/src/main/java/net/zetetic/tests/CipherMigrateTest.java).

---

<div class="post-metadata">

**Author:** ![gupta126](https://avatars.discourse-cdn.com/v4/letter/g/22d042/32.png) [@gupta126](https://discuss.zetetic.net/u/gupta126)\
**Post date:** [September 30, 2019, 6:41am UTC](https://discuss.zetetic.net/t/migrating-android-application-from-sqlcipher-3-to-4/3943/3 "2019-09-30T06:41:43Z")

</div>

HI @developernotes,  
As in given test suite I got to know that by using performing `cipher_migrate` script using `SQLiteDatabaseHook` will migrate database.  
Usecase: I have already published App with database v 1.0 and now App will get publish with database v 2, Now when app is gets updated OnUpgrade method get invoke where I am calling this `cipher_migrate` script but before that it gets crash  
so can you please suggest a way to achieve above usecase?

---

<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 30, 2019, 1:24pm UTC](https://discuss.zetetic.net/t/migrating-android-application-from-sqlcipher-3-to-4/3943/4 "2019-09-30T13:24:02Z")

</div>

Hi @gupta126

The `SQLiteOpenHelper` constructor will take a `SQLiteDatabaseHook` as a parameter which can be used to perform your `pragma cipher_migrate` operation.

---

<div class="post-metadata">

**Author:** ![gupta126](https://avatars.discourse-cdn.com/v4/letter/g/22d042/32.png) [@gupta126](https://discuss.zetetic.net/u/gupta126)\
**Post date:** [October 1, 2019, 5:09am UTC](https://discuss.zetetic.net/t/migrating-android-application-from-sqlcipher-3-to-4/3943/5 "2019-10-01T05:09:58Z")

</div>

Hi @developernotes,  
Thanks for the update, as you mention I can use `SQLiteDatabaseHook` as a parameter but, once the migration is successful then still it will call every time when instance will be created it so it might be a not good idea to use it in the constructor.

---

<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 1, 2019, 12:11pm UTC](https://discuss.zetetic.net/t/migrating-android-application-from-sqlcipher-3-to-4/3943/6 "2019-10-01T12:11:32Z")

</div>

Hello @gupta126

That is correct that you do not want to attempt the migration every time you open a connection. We recommend that you track the database file format upgrade state separately.
