davidmartos96 / sqflite_sqlcipher

SQLite flutter plugin
BSD 2-Clause "Simplified" License
99 stars 44 forks source link

File is not a database error on iOS after library upgrade #82

Closed martin-headspace closed 5 months ago

martin-headspace commented 11 months ago

I recently bumped one of my app's version of sqflite_sqlcipher on the fmdb_override branch to use the latest version.

My app was previously pointing to commit SHA 215d933108e6c0a11d87fd342e8cb04bb1d2e918 from back in September 17th, 2022 where we used a library without a password. However, now that I'm pointing to the very latest version I'm getting the following error:

Unknown error calling sqlite3_step (26: file is not a database) rs
Unknown error calling sqlite3_step (26: file is not a database) eu
DB Query: BEGIN EXCLUSIVE
Unknown error finalizing or resetting statement (26: file is not a database)
DB Query: BEGIN EXCLUSIVE
flutter: error DatabaseException(Error Domain=FMDatabase Code=26 "file is not a database" UserInfo={NSLocalizedDescription=file is not a database}) sql 'BEGIN EXCLUSIVE' args [] during open, closing...
Error calling sqlite3_step (1: cannot rollback - no transaction is active) SQLITE_ERROR

This is how my initialization code looks like:

var dbName = Environment.current.dbName;
    databasePath = await getDatabasesPath();
    database = await openDatabase(
      "$databasePath/$dbName",
      version: 122,
      onCreate: _onCreate,
      onUpgrade: _onUpgrade,
    );

Is there a pathway to fix this? I've read through the README and these issues:

But I couldn't find any conclusive solutions to my issue. Please help

davidmartos96 commented 11 months ago

What is the version of SQLCipher in your Podfile.lock?

martin-headspace commented 11 months ago

What is the version of SQLCipher in your Podfile.lock?

I believe it should be 4.4.2

- sqflite_sqlcipher (0.0.1):
    - Flutter
    - FMDB/SQLCipher (~> 2.7.5)
    - SQLCipher (= 4.4.2)
  - SQLCipher (4.4.2):
    - SQLCipher/standard (= 4.4.2)
  - SQLCipher/common (4.4.2)
  - SQLCipher/standard (4.4.2):
    - SQLCipher/common
davidmartos96 commented 11 months ago

File is not a database is thrown usually when you are trying to use an encrypted database without specifying a password. Can you double check that the database you are opening doesn't need a password? I would download that file into your machine and then open it there with sqlite3 CLI or with some GUI. If you are saying it doesn't have a password it should open externally. If it doesn't, somehow it now requires a password. If you go back right now to that previous version, does it work?

martin-headspace commented 11 months ago

File is not a database is thrown usually when you are trying to use an encrypted database without specifying a password. Can you double check that the database you are opening doesn't need a password? I would download that file into your machine and then open it there with sqlite3 CLI or with some GUI. If you are saying it doesn't have a password it should open externally. If it doesn't, somehow it now requires a password. If you go back right now to that previous version, does it work?

I'll try opening on an external sqlite3 tool and yes! If I roll back to that previous version, it does work w/o ever providing that password.

martin-headspace commented 11 months ago

I was able to open it on an external sqlite3 tool, so my guess is that since we created this DB using sqlcipher3, now that we're trying to open it using sqlcipher 4 it's become incompatible.

Could this be the case? If so, is there a known pathway to upgrade people's dBs?

davidmartos96 commented 11 months ago

I'm not totally understanding. Is there any particular reason you are not providing the password? Which database did you encrypt with SQLCipher 3?

To upgrade you have a couple commands that you would need to execute.

https://discuss.zetetic.net/t/upgrading-to-sqlcipher-4/3283

This package has always used 4.x that is what is confusing me about your setup.

davidmartos96 commented 11 months ago

Also, what's the output of the following query ? PRAGMA cipher_version

davidmartos96 commented 11 months ago

@martin-headspace Were you able to find the cause?

davidmartos96 commented 5 months ago

Closing. The last version of sqflite_sqlcipher doesn't need the fmdb_override workaround anymore.