Closed adad184 closed 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!
@adad184 How did it go? Can I close this?
yeah, i realized the "copying" may be the only solution for android.
we can close it. thx.
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.
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. :)
@adad184 Oh, I didn't know that :sweat_smile:
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?
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
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?
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.
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);
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?
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 :)
No problem!
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?