0

I'm working on a flutter project. I want to extract and add the database for backup, but I haven't been able to do it for days. I'm thinking about how I can do this. I wonder if I can do this. Is this possible? If not, should I do it? If this is not possible when using SQLite, what should I use?

I tried everything but I couldn't do it. If anyone has a sample project, please share it with me.

3
  • VACUUM INTO is probably the simplest way. Commented Dec 15, 2023 at 19:07
  • Do you have a sample code or a resource where I can learn how to do it? Commented Dec 15, 2023 at 20:16
  • It's just a SQL statement. See the linked documentation. Commented Dec 15, 2023 at 20:20

1 Answer 1

0

My understanding is that you just backup the files. The only trick to do this 'before' opening the database - as that locks the files.

Here is my backup script - note this won't work on web.

import 'dart:io';

import 'package:date_time_format/date_time_format.dart';

Future<void> backupDatabase(String pathToDatabase,
    {required int version}) async {
  final datePart =
      DateTimeFormat.format(DateTime.now(), format: 'Y.j.d.H.i.s');

  /// db file path with .bak and date/time/added
  final backupPath =
      '$pathToDatabase.$version.$datePart.bak';

  final dbFile = File(pathToDatabase);
  final backupFile = File(backupPath);


  if (dbFile.existsSync()) {
    await backupFile.writeAsBytes(await dbFile.readAsBytes());
    print('''
Database backup completed successfully: 
  Original Size: ${dbFile.lengthSync()}
  Backuped Size: ${backupFile.lengthSync()}
        ''');
  } else {
    print('Database file not found.');
  }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.