davidmartos96 / sqflite_sqlcipher

SQLite flutter plugin
BSD 2-Clause "Simplified" License
102 stars 46 forks source link

How to open a existed database in bundle? #17

Closed adad184 closed 4 years ago

adad184 commented 4 years ago

I have a encrypted databse file, so i plan to put it in "assets", but how can i open it?

I tried to open it by set the path to "assets/xx.db", obviously it's failed, and what i got is a new database with the same name and also empty.

so, how i can do it?

davidmartos96 commented 4 years ago

To access files in the assets folder from the device you need to copy the file to the device itself first. You can take a look at the example project in this repo. It has the common use cases to open a database. Let me know if you have any question. Glad to help!

davidmartos96 commented 4 years ago

@adad184 How did it go? Can I close this?

adad184 commented 4 years ago

yeah, i realized the "copying" may be the only solution for android.

we can close it. thx.

davidmartos96 commented 4 years ago

yeah, i realized the "copying" may be the only solution for android.

Actually it's also the same for iOS. It's just how the assets work.

adad184 commented 4 years ago

yeah, i realized the "copying" may be the only solution for android.

Actually it's also the same for iOS. It's just how the assets work.

well, in iOS, you can choose to put the file out of the assets, so you can access it directly. :)

davidmartos96 commented 4 years ago

@adad184 Oh, I didn't know that :sweat_smile:

adad184 commented 4 years ago

after I copy the database to databasesPath, the copying progress is no problem, but when I open the database, error occurs:

D/Sqflite ( 6739): Opening db in /data/user/0/com.xxx.xxx/databases/xxx.db with PRAGMA cipher_migrate
E/flutter ( 6739): [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: DatabaseException(open_failed /data/user/0/com.xxx.xxx/databases/xxx.db)
E/flutter ( 6739): #0      SqfliteSqlCipherDatabaseFactoryImpl.wrapDatabaseException 
package:sqflite_sqlcipher/src/factory_sql_cipher_impl.dart:44

I'm pretty sure the db file is good( I export it from emulator, then openned it with navicat), so I don't know why it opened failed, any suggestion?

adad184 commented 4 years ago

my code:

    // open the database
    var db = await openDatabase(
      tmpPath,
      // version: 1,
      password: 'xxxxxxxxxxxxxxxx',
      readOnly: true,
      // onCreate: (Database db, int version) async {},
      onConfigure: (db) async {},
      onOpen: (db) async {
        debugPrint("open db");

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

        response.forEach((element) {
          debugPrint(element.toString());
        });
      },
    );

@davidmartos96

davidmartos96 commented 4 years ago

From the logs it looks like it fails to open it as a SqlCipher 4 database and falls back to SqlCipher 3, which fails as well. How are you copying the database? Can you share the code?

davidmartos96 commented 4 years ago

You can also try cloning the repo and trying out the example folder project and modify the test that uses an assets db using your own database.

adad184 commented 4 years ago

From the logs it looks like it fails to open it as a SqlCipher 4 database and falls back to SqlCipher 3, which fails as well. How are you copying the database? Can you share the code?

just copy the code from the example:"

    // Copy from asset
    var data = await rootBundle.load("assets/db/xxx.db");
    List<int> bytes = data.buffer.asUint8List(data.offsetInBytes, data.lengthInBytes);
    // Write and flush the bytes written
    await File(tmpPath).writeAsBytes(bytes, flush: true);
davidmartos96 commented 4 years ago

Also, another question. Is your database already encrypted or are you putting a plain db in assets and trying to open it with a password?

adad184 commented 4 years ago

Also, another question. Is your database already encrypted or are you putting a plain db in assets and trying to open it with a password?

damn... the db file is not encrypted, the db was supposed to encrypted but someone gave me a pain db. what a waste of time....

thx for your patient. all problems seem to be solved, by now :)

davidmartos96 commented 4 years ago

No problem!