Database created by low version SQLCipher sqlite3_backup on a higher version SQLCipher results in Error 11

Hi,
I am currently utilizing sqlite3_backup to upgrade an encrypted version 3 database to version 4 while online. When I create a version 3 encrypted database using SQLCipher version “4.8.0” with PRAGMA cipher_compatibility=3 and subsequently transfer it to a version 4 database with sqlite3_backup, the process operates without issues.

However, I discovered a problem when creating an encrypted version 3 database using an older SQLCipher version such as 3.4.1. If I then attempt an upgrade to version 4 with sqlite3_backup using version 4.8.0, the process completes without error, but reopening the database results in an error: “SQLite Error [11]: malformed database schema (orphan index in ‘PRAGMA integrity_check(10)’)”.

Steps to reproduce the issue:

  1. Create a test database using SQLCipher version lower than 4.3.0, such as 3.4.1.
  2. With SQLCipher version 4.8.0, open the test database using PRAGMA cipher_compatibility=3, create a new backup database without PRAGMA cipher_compatibility=3, then sqlite3_backup the test database to the backup database.
  3. Close and reopen the backup database, then execute a select query for testing.

Thank you for your attention to this matter. Your assistance is greatly appreciated.

Hello @Galois - using sqlite3_backup to perform migration is not supported. There are only two supported methods for migration:

  1. PRAGMA cipher_migrate
  2. sqlcipher_export()

The SQLite backup API is only supported in a limited capacity, and that is for encrypted-to-encrypted or plaintext-to-plaintext backups with the same encryption settings.

Thank you for your response. That’s helpful, However, it seems that after creating a version 3 encrypted database using scipher_compatibility=3 in SQLCipher 4.8, the process of backing it up to a version 4 database using sqlite3_backup appears to work correctly.