Can I export my sqlite database from Flutter?

5,664

Solution 1

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.

Solution 2

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!

Share:
5,664
AlexPad
Author by

AlexPad

Let's explore together, let's grow together!

Updated on December 11, 2022

Comments

  • AlexPad
    AlexPad over 1 year

    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)