Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SqfliteDatabaseException DatabaseException(open_failed /var/mobile/Containers/Data/Application/.../Documents/database_name.db) #99

Open
amrLLSE opened this issue Sep 24, 2024 · 22 comments

Comments

@amrLLSE
Copy link

amrLLSE commented Sep 24, 2024

I'm facing this on the latest version
SqfliteDatabaseException DatabaseException(open_failed/var/mobile/Containers/Data/Application/.../Documents/database_name.db)

@davidmartos96
Copy link
Owner

That can happen when you are providing the wrong password to an encrypted database. Are you opening a database bundled in the app assets or are you encrypting a fresh database?

@amrLLSE
Copy link
Author

amrLLSE commented Sep 24, 2024

@davidmartos96 Fresh database, but I'm sure 100% that the password is correct. Now I'm also investigating in this and if I have reached to the root cause I will mention it here. But if you have any other ideas why this may happen just let me know.

@davidmartos96
Copy link
Owner

I would double check if you have sqlcipher correctly installed. You can open a memory database with sqflite without password and then run "PRAGMA cipher_version" on the database. You should see SQLCipher and the version number. If you don't, it means that something is not correctly configured

@amrLLSE
Copy link
Author

amrLLSE commented Sep 24, 2024

@davidmartos96 Actually I have run "PRAGMA cipher_version" and it prints successfully "SQLCipher Version: 4.5.7 community".

@davidmartos96
Copy link
Owner

davidmartos96 commented Sep 24, 2024

In that case I can only think the following:

  • Password is incorrect
  • The database was previously encrypted with SQLCipher 3.x, which is not directly compatible with 4.x

I'd try deleting the file in the device and try fresh with your latest code. Maybe it was previously encrypted with a different password.

You can also check the SQLCipher related tests (in the lib folder) in the example app in the repository

@amrLLSE
Copy link
Author

amrLLSE commented Sep 24, 2024

@davidmartos96 Okay will check, but this plugin is always using 4x version and I didn't use anything else to open the database.

Thanks for you quick replies and support

@marijam26
Copy link

I'm encountering the same error.

@davidmartos96
Copy link
Owner

@marijam26 would you be able to create a reproduction demo app?

@amrLLSE
Copy link
Author

amrLLSE commented Feb 19, 2025

@davidmartos96 Also still facing it, this is totally random so unfortunately I can't show a demo for it. Nearly I have tried everything to catch the scenario for this but as mentioned, it is random.

@nrsimha
Copy link

nrsimha commented Mar 8, 2025

I have same issue.

Using this code:

        _db = await sqflite_cipher.openDatabase(
          filename,
          password: GDATABASE_PASSWORD,
        );

When running same Flutter app on Macos it runs properly I can open passworded sqlite file.

When running on Android with same code (same password) and same sqlite file it crashes on above function.

I/flutter (10789): DatabaseException(open_failed /file/path/to/sqlite/file)
Application finished.

@davidmartos96
Copy link
Owner

@nrsimha What iOS version has the device that fails?

@nrsimha
Copy link

nrsimha commented Mar 8, 2025

@davidmartos96 It has not iOS version as it is Android 13 device. Didn't try on iOS yet.

@davidmartos96
Copy link
Owner

@nrsimha Sorry, I thought this was iOS only.

What do you get after running this? #99 (comment)

Without a reproduction example/project I cannot see what could be wrong.

@nrsimha
Copy link

nrsimha commented Mar 8, 2025

What do you get after running this? #99 (comment)

Running on both Android and Macos I am getting this result:

SQLCipher is installed. Version: 4.5.7 community

I run this code to test it:

import 'package:sqflite_sqlcipher/sqflite.dart';
// Open an in-memory database (without encryption)
Database db = await openDatabase(':memory:');

// Run the PRAGMA cipher_version query
List<Map<String, dynamic>> result = await db.rawQuery(
  "PRAGMA cipher_version;",
);

// Close the database
await db.close();

// Print the result
if (result.isNotEmpty && result.first.containsKey('cipher_version')) {
  print(
    "SQLCipher is installed. Version: ${result.first['cipher_version']}",
  );
} else {
  print("SQLCipher is NOT installed or not configured correctly.");
}

@nrsimha
Copy link

nrsimha commented Mar 8, 2025

Without a reproduction example/project I cannot see what could be wrong.

I tried to make minimal project with example and it works on Android now.
So it seems there is an issue with my project.

@davidmartos96
Copy link
Owner

@nrsimha Does the destination file parent folder exist before calling open?

@nrsimha
Copy link

nrsimha commented Mar 8, 2025

@davidmartos96 Issue is somehow related to conflicting with sqflite. When user select file it first try to open it with sqflite:

import 'package:sqflite/sqflite.dart' as sqflite;
import 'package:sqflite_sqlcipher/sqflite.dart' as sqflite_cipher;
    try {
      // Trying to open it as an unencrypted database
      _db = await sqflite.openDatabase(filename!);
      // need to run query to find if db is not encrypted
      // setMeta function tries to open db_meta table and get some data
      // if database is passworded it makes exception and in catch it should
      // open with password through sqflite_cipher
      await setMeta();
      isOpenAndValid = true;
    } catch (e) {
      await _db!.close();
      // will try to open file through sqflite_cipher as it seems
      // file is password protected
      try {
        _db = await sqflite_cipher.openDatabase(
          filename!,
          password: GDATABASE_PASSWORD,
        );
        await setMeta();
        isOpenAndValid = true;
      } catch (e) {
        throw Exception(
          "Failed to open database. It might be corrupted or using a different password.",
        );
      }
    }

For some reason it crashes on Android when I try to run sqflite_cipher.openDatabase() after running sqflite.openDatabase() before.

If I try first to open with password it opens nicely.

@davidmartos96
Copy link
Owner

@nrsimha Interesting, are you able to reproduce it in the demo you were building?

@nrsimha
Copy link

nrsimha commented Mar 8, 2025

Yes, it is definitely caused by trying to open that file with sqflite opening as when I checked filesize before it was 328MB and after that it was 12kB. So it somehow deleted whole database and when I tried to open it with sqflite_sqlcipher it crashed as file was damaged.

@nrsimha
Copy link

nrsimha commented Mar 8, 2025

Interesting, are you able to reproduce it in the demo you were building?

In demo was working as I tried to open file only through sqflite_sqlcipher.

Fix was to set readOnly: true while trying to figure out if file is password protected or not. With readonly sqflite didn't damage the file and it could be opened with sqflite_sqlcipher without any issues.

So my issue was not caused by sqflite_sqlcipher, but by sqflite.

Thank you for sqflite_sqlcipher and your quick responses!

@davidmartos96
Copy link
Owner

@nrsimha Just so you know, sqlcipher is compatible with regular sqlite if you provide an empty password.
You could read the exception you get by providing an empty password to the target database and act accordingly.

@nrsimha
Copy link

nrsimha commented Mar 8, 2025

@nrsimha Just so you know, sqlcipher is compatible with regular sqlite if you provide an empty password. You could read the exception you get by providing an empty password to the target database and act accordingly.

Cool! Didn't know that. It makes my code cleaner. Still need to use readonly for testing though, but that is not a problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants