Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I export my sqlite database from Flutter?

There are various articles on how to import data on the internal database from a csv, from another database etc .. but I haven't found anyone explaining how to export the flutter database.

The goal is to create a backup for each cell phone. (So I need to understand where it is located for create a backup)

like image 483
AlexPad Avatar asked Mar 03 '23 21:03

AlexPad


2 Answers

I assume you are using sqflite plugin for SQLite operations and path_provider for storage. The path of the database can be found using

String path = await getDatabasesPath(); // which is data/data/<package_name>/databases

Additionally, sqflite plugin doesn't provide any way to import/export database, here is an open issue, if you really want to do it, you will have to do it natively using MethodChannel, here is the solution for Android and AFAIK there is no way to do it in iOS.

like image 139
CopsOnRoad Avatar answered Mar 07 '23 10:03

CopsOnRoad


if you use Sqlite to create database:

Step 1: When you create database, you set a directory for it, you can use path_provider like this:

var dir = await getApplicationDocumentsDirectory();
_dbPath = dir.path + '/$dbName';

so now you know what directory and path it is.

Step 2: Then use flutter_archive plugin to zip the file (This will zip the file in a directory, which is your db);

Step 3: use flutter_email_sender to send it by email, like this:

final email = Email(
      body: 'content',
      subject: 'content',
      recipients: ['email'],
      cc: ['email'],
      attachmentPaths: [exportPath],
      isHTML: false,
    );
    await FlutterEmailSender.send(email);

Your need to provide exportPath, which is the zip file path you set up.

It worked for us, hope this will help other people!

like image 29
lu tang Avatar answered Mar 07 '23 09:03

lu tang