4

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)

AlexPad
  • 10,364
  • 3
  • 38
  • 48

4 Answers4

5

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.

CopsOnRoad
  • 237,138
  • 77
  • 654
  • 440
4

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!

Kavitha Karunakaran
  • 1,340
  • 1
  • 17
  • 32
lu tang
  • 51
  • 3
  • Actually when your database is open, you can not archive it I think and you need to close the database first which is a bit challenging like how you can do it when you is still running? – Haroon khan Oct 24 '21 at 07:52
0

Usually on Android, databases are stored at /data/data/your.app.signature.here/databases/. But I don't know if that's different on flutter apps.

Hope that helps.

0

dependencies:

 sqflite: ^2.0.0+3
 path_provider: ^2.0.11
 permission_handler: ^10.0.0

Export ( back up)

To export SQFLite database , I came across some errors , some of the error are

  1. FileSystemException: Cannot open file, path
  2. error: permission denied, errno = 13
  3. etc........

I want to export my Database into Download folder that is ,

this is my Database path /data/user/0/com.example.reminder_app/databases/notes.db , it's a application directory path so my aim is to export notes.db file into this path /storage/emulated/0/Download/

  1. Expanding dBToCopy functions , this function will give path of Database
  Future<File> dBToCopy() async {
    final db = await instance.database;
    final dbPath = await getDatabasesPath();
    var afile = File(dbPath);
    return afile;
  }

  1. full code bellow

dbExportToDownloadFolder() async {

 File result = await NotesDatabase.instance.dBToCopy();
 print("lllllllllllllllllll ${result.absolute.path}"); 
        
 Directory documentsDirectory = 
           Directory("storage/emulated/0/Download/");

 String newPath = join(documentsDirectory.absolute.path + 'abcde.db');

 File b = 
      File("/data/user/0/com.example.reminder_app/databases/notes.db");
                         
 if ( await Permission.storage.request().isGranted &&
      await Permission.accessMediaLocation.request().isGranted &&
      await Permission.manageExternalStorage.request().isGranted )
       {
         File a = await b.copy(newPath);
        } else {
             print("No Permission Granted");
               }
}
                 

Note

File result = await NotesDatabase.instance.dBToCopy();

print("lllllllllllllllllll ${result.absolute.path}"); 

OutPut print

lllllllllllllllllll /data/user/0/com.example.reminder_app/databases

this result file not contain the notes.db file , only contain this path /data/user/0/com.example.reminder_app/databases

To get the DatabaseFile

File b = File("/data/user/0/com.example.reminder_app/databases/notes.db");

                or 

File b = File("${result.path}"+"/notes.db");

so using the file b we can copy the file to Download folder file that is abcde.db

To do that create a file in Download , that is abcde.db

 Directory documentsDirectory = Directory("storage/emulated/0/Download/");

 String newPath = join(documentsDirectory.absolute.path + 'abcde.db');

and using the copy method , to copy one file to another file

 File a = await b.copy(newPath);

Note

If you are getting permission denied errors and OS errors please add all permission in manifest , and using permission_handler allow all permissions

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />

<uses-permission android:name="android.permission.ACCESS_MEDIA_LOCATION"/>

after the copying , A new file created in Download folder that is abcde.db

Jinto Joseph
  • 947
  • 6
  • 23