davidmartos96 / sqflite_sqlcipher

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

open_failed #47

Closed YingW1012 closed 3 years ago

YingW1012 commented 3 years ago

Failed to open encrypted database The database was created on the IOS platform Sqlitestudio can be opened What's going on

I/flutter ( 7114): /data/user/0/com.ying.product_show/app_flutter/factoryNew.db D/Sqflite ( 7114): Opening db in /data/user/0/com.ying.product_show/app_flutter/factoryNew.db with PRAGMA cipher_migrate E/flutter ( 7114): [ERROR:flutter/lib/ui/ui_dart_state.cc(199)] Unhandled Exception: DatabaseException(open_failed /data/user/0/com.ying.product_show/app_flutter/factoryNew.db) E/flutter ( 7114): #0 SqfliteSqlCipherDatabaseFactoryImpl.wrapDatabaseException (package:sqflite_sqlcipher/src/factory_sql_cipher_impl.dart:44:9) E/flutter ( 7114): E/flutter ( 7114): #1 SqfliteDatabaseMixin.openDatabase (package:sqflite_common/src/database_mixin.dart:558:9) E/flutter ( 7114): E/flutter ( 7114): #2 SqfliteDatabaseMixin.doOpen (package:sqflite_common/src/database_mixin.dart:651:22) E/flutter ( 7114): E/flutter ( 7114): #3 SqfliteDatabaseOpenHelper.openDatabase (package:sqflite_common/src/database.dart:44:7) E/flutter ( 7114): E/flutter ( 7114): #4 SqfliteDatabaseFactoryMixin.openDatabase. (package:sqflite_common/src/factory_mixin.dart:112:18) E/flutter ( 7114): E/flutter ( 7114): #5 ReentrantLock.synchronized. (package:synchronized/src/reentrant_lock.dart:37:18) E/flutter ( 7114): E/flutter ( 7114): #6 BasicLock.synchronized (package:synchronized/src/basic_lock.dart:33:16) E/flutter ( 7114): E/flutter ( 7114): #7 Provider.init (package:product_show/manager/db/provider.dart:99:14) E/flutter ( 7114): E/flutter ( 7114): #8 main (package:product_show/main.dart:13:3) E/flutter ( 7114): E/flutter ( 7114): Application finished.

davidmartos96 commented 3 years ago

What do you mean it was created on iOS? Could you try opening it with an empty password, maybe the db you are using is not encrypted? Also it would be helpful to know the device and Android version you are using.

YingW1012 commented 3 years ago

The password is right

The encrypted database is created with IOS app

Can open with SQLite studio(an app on Mac OS)

It was tested using the Android emulator

The API used is 30

davidmartos96 commented 3 years ago

Is it a database that you copy from assets? Can I see the code where you do that? What I meant with the DB being created on iOS is, how did you extract it from the app storage to now open it on Android or in Sqlite Studio

YingW1012 commented 3 years ago

Using file browsing tool directly

Copy dB from iPhone app document directory

Put it in the corresponding directory of Android simulator

Then open it from the flutter project

Directory documentsDir = await getApplicationDocumentsDirectory(); String documentsPath = documentsDir.path; String path = '$documentsPath/xxxx.db'; print(path); var db = await openDatabase( path, // version: 1, password: 'XXXX', readOnly: true, // onCreate: (Database db, int version) async {}, onConfigure: (db) async {}, onOpen: (db) async { print("open db");

    var response = await db.rawQuery("SELECT name FROM sqlite_master WHERE type = 'table'");

    response.forEach((element) {
      print(element.toString());
    });
  },
);
davidmartos96 commented 3 years ago

Now that I see the code, it reminds me of an issue I had on my app long ago for using readOnly. Can you try disabling the readOnly? Unfortunately the error is not very helpful here. If that doesn't work the next best thing you can do is create a reproducible example with a dummy database with the password you like so that I can try it.

YingW1012 commented 3 years ago

I found the problem

The DB can be opened by placing it in the databases folder

davidmartos96 commented 3 years ago

I found the problem

The DB can be opened by placing it in the databases folder

That's odd. I have some code that opens a database in a temporary directory and it works correctly.