# Cannot open encrypted database with SQLCipher 4

**URL:** <https://discuss.zetetic.net/t/cannot-open-encrypted-database-with-sqlcipher-4/3654>\
**Category:** SQLCipher\
**Created:** [May 10, 2019, 1:41am UTC](https://discuss.zetetic.net/t/cannot-open-encrypted-database-with-sqlcipher-4/3654 "2019-05-10T01:41:19Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![jdleung](https://avatars.discourse-cdn.com/v4/letter/j/58f4c7/32.png) [@jdleung](https://discuss.zetetic.net/u/jdleung)\
**Post date:** [May 10, 2019, 1:41am UTC](https://discuss.zetetic.net/t/cannot-open-encrypted-database-with-sqlcipher-4/3654/1 "2019-05-10T01:41:19Z")

</div>

Using CocoaPods, SQLCipher 4.0, Swift 4. Database created and encrypted(with SQLCipher 4) by DB Browser for SQLite.

SQLCipher 4 **works well with database without encryption** , but it fails to open an encrypted database. It shows error message: **Error setting key: not an error** , **Error retrieiving cipher\_version: no more rows available**

Code:  
`rc = sqlite3_key(enneaDB, myKey, Int32(myKey.utf8CString.count))`

The above operations always work well with SQLCipher 3 in other Apps.

Edited:  
When App running on iOS 10 simulator, App crashes with this error message:  
**dyld: Symbol not found: \_sqlite3\_key**  
**Expected in: /Library/Developer/CoreSimulator/Profiles/Runtimes/iOS 10.0.simruntime/Contents/Resources/RuntimeRoot/usr/lib/libsqlite3.dylib**

Any help? Thanks.

---

<div class="post-metadata">

**Author:** ![mmoore](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.zetetic.net/mmoore/32/858_2.png) [@mmoore](https://discuss.zetetic.net/u/mmoore)\
**Post date:** [May 10, 2019, 2:34pm UTC](https://discuss.zetetic.net/t/cannot-open-encrypted-database-with-sqlcipher-4/3654/2 "2019-05-10T14:34:40Z")

</div>

Hi @jdleung

A few questions + suggestions:

1. Are you including any other dependencies/pods that may be including SQLite?
2. Can you verify that you are in fact using SQLCipher by seeing what’s returned from `PRAGMA cipher_version;` ?
3. Can you provide your Podfile and/or a sample project that reproduces the issue?
4. Can you provide a sample database (with no sensitive information) that reproduces the issue?
5. What build/version of DB Browser for SQLite are you using?
6. If you create the database within your swift code rather than using DB Browser for SQLite does the issue still exist?

---

<div class="post-metadata">

**Author:** ![jdleung](https://avatars.discourse-cdn.com/v4/letter/j/58f4c7/32.png) [@jdleung](https://discuss.zetetic.net/u/jdleung)\
**Post date:** [May 10, 2019, 3:48pm UTC](https://discuss.zetetic.net/t/cannot-open-encrypted-database-with-sqlcipher-4/3654/3 "2019-05-10T15:48:18Z")

</div>

Hi @mmoore

1. There is no other dependencies/pods including SQLite.

2. The cipher\_version: “unknown error” on iOS 10, I have not tested on other iOS version

3. Here is the Podfile:  
platform :ios, ‘10.0’

4. I will upload it tomorrow later.

5. DB Browser for SQLite version is 3.11.2 Based on SQLCipher 4.1.0

6. I’ll try to do this later, but can it create an encrypted database without sqlite3\_key working?

Thanks.

---

<div class="post-metadata">

**Author:** ![mmoore](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.zetetic.net/mmoore/32/858_2.png) [@mmoore](https://discuss.zetetic.net/u/mmoore)\
**Post date:** [May 10, 2019, 4:42pm UTC](https://discuss.zetetic.net/t/cannot-open-encrypted-database-with-sqlcipher-4/3654/4 "2019-05-10T16:42:50Z")

</div>

@jdleung

> [@jdleung](#):
>
> pod ‘Google-Mobile-Ads-SDK’

This is the culprit. If you look at their pod spec, they are linking sqlite3.

> <https://github.com/CocoaPods/Specs/blob/master/Specs/5/9/a/Google-Mobile-Ads-SDK/7.43.1/Google-Mobile-Ads-SDK.podspec.json#L26>

If both SQLCipher and a standard version of SQLite are linked, this can lead to undefined behavior.

Our recommendation would be to remove any other pods that depend on SQLite.

Alternatively, there is some guidance in these posts on how to ensure SQLCipher is preferred when linking:

**Note: This is considered unsupported**

> [@Databases not keying properly in iOS 10?](https://discuss.zetetic.net/t/databases-not-keying-properly-in-ios-10/1666/10):
>
> @sjlombardo after doing as you said, the build now fails with the following error ld: library not found for -lsqlcipher clang: error: linker command failed with exit code 1 (use -v to see invocation)

> [@Important Advisory: SQLCipher with Xcode 8 and new SDKs](https://discuss.zetetic.net/t/important-advisory-sqlcipher-with-xcode-8-and-new-sdks/1688):
>
> Note: Updated to clarify that this guidance applies to all projects built with Xcode 8, including for non-iOS targets). Apple has recently released XCode 8, along with new SDKs (e.g. iOS 10, watchOS 3, mac OS, tvOS), with many new features and enhancements. The majority of projects that include SQLCipher should have no problems after upgrading. However, there are some important changes and edge cases, so we have prepared the following important recommendations for all SQLCipher developers upgra…

Specifically in your scenario, since you’re using cocoa pods with `use_frameworks` you’ll want to put this as the first item in your target’s Build Settings \> Other Linker Flags: `-framework SQLCipher` as recommended in the second post. We’d also recommend following the guidance in that second post related to ensuring that SQLCipher is properly linked at runtime.

---

<div class="post-metadata">

**Author:** ![jdleung](https://avatars.discourse-cdn.com/v4/letter/j/58f4c7/32.png) [@jdleung](https://discuss.zetetic.net/u/jdleung)\
**Post date:** [May 11, 2019, 12:24am UTC](https://discuss.zetetic.net/t/cannot-open-encrypted-database-with-sqlcipher-4/3654/5 "2019-05-11T00:24:27Z")

</div>

Hello, @mmoore

It works like a magic after removing Google Ads from CocoaPods.

And then I added Google Ads again in Podfile, and inserted `-framework SQLCipher` in Project \> Other Linker Flags, Both SQLCipher and Google Ads work perfect.

Thank you so much!

---

<div class="post-metadata">

**Author:** ![mmoore](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.zetetic.net/mmoore/32/858_2.png) [@mmoore](https://discuss.zetetic.net/u/mmoore)\
**Post date:** [May 13, 2019, 2:16pm UTC](https://discuss.zetetic.net/t/cannot-open-encrypted-database-with-sqlcipher-4/3654/6 "2019-05-13T14:16:46Z")

</div>

@jdleung

Glad it’s working well for you now.

---

<div class="post-metadata">

**Author:** ![micyat](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@micyat](https://discuss.zetetic.net/u/micyat)\
**Post date:** [July 25, 2019, 6:06pm UTC](https://discuss.zetetic.net/t/cannot-open-encrypted-database-with-sqlcipher-4/3654/7 "2019-07-25T18:06:36Z")

</div>

@mmoore awesome solution! I can’t believe this subtle problem has costed hours to locate.

My problem was very similar, by using Google library (again) at cocoapod. It secretly induced a sqlite library dependency which cause mislink at runtime. These kind of runtime linking issue is again very subtle and extremely hard to be troubleshooted. Especially when the problem is not noticed right away.

While @sjlombardo has written a comprehensive article about this issue. Although it is rare to meet, I suggest it should be put at FAQ or common mistake section to avoid fellows from suffering if they are so lucky to meet this problem.

> [@Important Advisory: SQLCipher with Xcode 8 and new SDKs](https://discuss.zetetic.net/t/important-advisory-sqlcipher-with-xcode-8-and-new-sdks/1688):
>
> Note: Updated to clarify that this guidance applies to all projects built with Xcode 8, including for non-iOS targets). Apple has recently released XCode 8, along with new SDKs (e.g. iOS 10, watchOS 3, mac OS, tvOS), with many new features and enhancements. The majority of projects that include SQLCipher should have no problems after upgrading. However, there are some important changes and edge cases, so we have prepared the following important recommendations for all SQLCipher developers upgra…

Anyway, appropriate the good work.

---

<div class="post-metadata">

**Author:** ![mmoore](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.zetetic.net/mmoore/32/858_2.png) [@mmoore](https://discuss.zetetic.net/u/mmoore)\
**Post date:** [July 26, 2019, 7:12pm UTC](https://discuss.zetetic.net/t/cannot-open-encrypted-database-with-sqlcipher-4/3654/8 "2019-07-26T19:12:37Z")

</div>

@micyat

Thanks for the recommendation. We’ve added a note below the integration options on the iOS Tutorial page here: [https://www.zetetic.net/sqlcipher/ios-tutorial/](https://www.zetetic.net/sqlcipher/ios-tutorial/) with additional details and linking to the article.
