# Sqlcipher migration fails from 3.x to 4.x in iOS

**URL:** <https://discuss.zetetic.net/t/sqlcipher-migration-fails-from-3-x-to-4-x-in-ios/4572>\
**Category:** SQLCipher\
**Created:** [August 28, 2020, 11:26am UTC](https://discuss.zetetic.net/t/sqlcipher-migration-fails-from-3-x-to-4-x-in-ios/4572 "2020-08-28T11:26:22Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![vittalpai](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.zetetic.net/vittalpai/32/1451_2.png) [@vittalpai](https://discuss.zetetic.net/u/vittalpai)\
**Post date:** [August 28, 2020, 11:26am UTC](https://discuss.zetetic.net/t/sqlcipher-migration-fails-from-3-x-to-4-x-in-ios/4572/1 "2020-08-28T11:26:22Z")

</div>

Hi All,

I am trying to migrate my SQLCipher encrypted DB in iOS, it’s ending up with `Create statement failed, message: [file is not a database]` whenever I tried to run a simple query after the Migration.

I followed the steps as mentioned in the following doc [Upgrading to SQLCipher 4](https://discuss.zetetic.net/t/upgrading-to-sqlcipher-4/3283)

```auto
    PRAGMA key = "x'a651949519df3393a3dfe81b2a537b500914245974b35b780369b0fbadd36e49'";
    PRAGMA cipher_migrate;

```

Executed the above commands using following API  
sqlite3\_exec(); -\> Returns 0

However, running a simple query in the DB after migration will end up in the above error.

Sample 3.x DB which I am trying with [vittal.sqlite.zip](https://discuss.zetetic.net/uploads/short-url/umJmoO4Bw1GXZxjpfchFKqlEEZ3.zip) (3.2 KB)

Key for the sample DB is pasted above

Any help is much appreciated.

Thanks,

---

<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:** [August 28, 2020, 2:04pm UTC](https://discuss.zetetic.net/t/sqlcipher-migration-fails-from-3-x-to-4-x-in-ios/4572/2 "2020-08-28T14:04:33Z")

</div>

Hi @vittalpai

Do you have any other non-default cipher settings being set to access your SQLCipher 3 database? The reason I ask is that I am unable to migrate your database using SQLCipher 4 from the command line:

 ![Screen Shot 2020-08-28 at 9.00.02 AM](https://us1.discourse-cdn.com/flex016/uploads/zetetic/original/2X/2/2635555ba7e1d2d7de098de5a196c06b8b3812c4.png)

> [@vittalpai](#):
>
> Executed the above commands using following API  
> sqlite3\_exec(); → Returns 0

`sqlite3_exec` may be returning `0`, however, the result of the statement is actually reported via the callback function provided to `sqlite3_exec`. You can try passing a reference to a function like this to confirm on your end:

```auto
int result_callback(void *arg, int argc, char **argv, char** azColName){
  for(int index = 0; index < argc; index++) {
    printf("result:%s:%s", azColName[index], argv[index]);
  }
  printf("\n");
  return 0;
}
rc = sqlite3_exec(db, "PRAGMA cipher_migrate", &result_callback, NULL, NULL);

```

---

<div class="post-metadata">

**Author:** ![vittalpai](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.zetetic.net/vittalpai/32/1451_2.png) [@vittalpai](https://discuss.zetetic.net/u/vittalpai)\
**Post date:** [August 31, 2020, 10:12am UTC](https://discuss.zetetic.net/t/sqlcipher-migration-fails-from-3-x-to-4-x-in-ios/4572/3 "2020-08-31T10:12:18Z")

</div>

Hi [developernotes](https://discuss.zetetic.net/u/developernotes),

Thanks for your quick reply, I have tried by adding the callback and am getting the 1 error code in the call back.

Also I am able to access this DB if I run it in backward compatibility mode.

```auto
PRAGMA cipher_compatibility = 3;

```

Any idea on how to resolve this ? Also Can you provide me objective C code snippet for custom export migration?

---

<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:** [August 31, 2020, 8:42pm UTC](https://discuss.zetetic.net/t/sqlcipher-migration-fails-from-3-x-to-4-x-in-ios/4572/4 "2020-08-31T20:42:46Z")

</div>

Hi @vittalpai

I am not able to access the database you uploaded above with the provided password and the `cipher_compatibility` set to `3`.

---

<div class="post-metadata">

**Author:** ![vittalpai](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.zetetic.net/vittalpai/32/1451_2.png) [@vittalpai](https://discuss.zetetic.net/u/vittalpai)\
**Post date:** [September 1, 2020, 10:40am UTC](https://discuss.zetetic.net/t/sqlcipher-migration-fails-from-3-x-to-4-x-in-ios/4572/5 "2020-09-01T10:40:31Z")

</div>

Hi @developernotes,

Sorry, here is the DB which works fine for me with `cipher_compatibility` set to `3`. However, it fails if I try to migrate with `cipher_migrate`.

[vittal.sqlite.zip](https://discuss.zetetic.net/uploads/short-url/mBsnpkH4O7ALrK2UyNV845zbwI4.zip) (3.2 KB)

```auto
PRAGMA key = "x'28185f415a2303dea6e695487dd3ea0b7a773aa733e9e71720a134d7d77cdbef'";

```

---

<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:** [September 2, 2020, 1:15pm UTC](https://discuss.zetetic.net/t/sqlcipher-migration-fails-from-3-x-to-4-x-in-ios/4572/6 "2020-09-02T13:15:38Z")

</div>

Hello @vittalpai - I downloaded the file you attached and was able to use cipher\_migrate to update it to SQLCipher 4 without any problems. This tends to point to an application issue. Can you verify that you have available storage space for migration and that both the database and the folder it is stored in are writable by the application?

---

<div class="post-metadata">

**Author:** ![vittalpai](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.zetetic.net/vittalpai/32/1451_2.png) [@vittalpai](https://discuss.zetetic.net/u/vittalpai)\
**Post date:** [September 3, 2020, 1:19pm UTC](https://discuss.zetetic.net/t/sqlcipher-migration-fails-from-3-x-to-4-x-in-ios/4572/7 "2020-09-03T13:19:39Z")

</div>

Thanks a lot, @developernotes and @sjlombardo.

The issue got resolved. My app code was asynchronous and It was checking for `sqlite3_exec` return code.

Changing the app code to run synchronously esp wait till it returns the value from callback of `sqlite3_exec` API resolved my issue.

Thanks,

---

<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 3, 2020, 6:51pm UTC](https://discuss.zetetic.net/t/sqlcipher-migration-fails-from-3-x-to-4-x-in-ios/4572/8 "2020-09-03T18:51:44Z")

</div>

Hi @vittalpai

Thanks for the update, we are happy to hear you were able to resolve the issue.
