Skip to content

Issues opening a database using SQLCipher #302

@ManuelRauber

Description

@ManuelRauber

Hi,

here's my setup:

  • Flutter 3.29.3
  • iOS 18.4
  • sqlcipher_flutter_libs: ^0.6.6
  • sqlite3: ^2.7.5

Code sample:

Future main() async {
  WidgetsFlutterBinding.ensureInitialized();

  final applicationDocumentsDirectory = await getApplicationDocumentsDirectory();
  final dbPath = path.join(applicationDocumentsDirectory.path, 'MyDb.sqlite');

  final db = sqlite3.open(dbPath);

  final cipherVersion = db.select('PRAGMA cipher_version;');

  for (final row in cipherVersion) {
    print(row);
  }

  if (db.select('PRAGMA cipher_version;').isEmpty) {
    // Make sure that we're actually using SQLCipher, since the pragma used to encrypt
    // databases just fails silently with regular sqlite3 (meaning that we'd accidentally
    // use plaintext databases).
    throw StateError('SQLCipher library is not available, please check your dependencies!');
  }

  // Set the encryption key for the database
  db.execute("PRAGMA key = '<pw>;");
  final result = db.select("SELECT count(*) as cnt FROM sqlite_master;");

  for (final row in result) {
    print(row['cnt']);
  }

  db.dispose();

The print of row['cnt'] is always 0:

flutter: {cipher_version: 4.8.0 community}
flutter: 0

When I open the database locally using the sqlcipher binary (version 4.6.1) or in JetBrains DataGrip, I can successfully open the database and run the SELECT statement returning the correct number of rows.

The password only contains letters and numbers, no special characters.

I'm unsure what I could do at the moment to open the same database via Dart/Flutter.

Any tip is appreciated!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions