繁体   English   中英

备份和恢复 Flutter SQFlite 数据库

[英]Backup and Restore Flutter SQFlite database

我想将我的 Flutter SQFlite 数据库备份到移动设备。 我需要在现有应用程序中恢复数据库。 我在互联网上搜索,但没有得到任何合适的教程或文档。 有谁请帮忙!!! 提前致谢。

演示应用

有关完整的详细信息,请转到链接

ElevatedButton(
          onPressed: () async {
            final dbFolder = await getDatabasesPath();
            File source1 = File('$dbFolder/doggie_database.db');

            Directory copyTo =
                Directory("storage/emulated/0/Sqlite Backup");
            if ((await copyTo.exists())) {
              // print("Path exist");
              var status = await Permission.storage.status;
              if (!status.isGranted) {
                await Permission.storage.request();
              }
            } else {
              print("not exist");
              if (await Permission.storage.request().isGranted) {
                // Either the permission was already granted before or the user just granted it.
                await copyTo.create();
              } else {
                print('Please give permission');
              }
            }

            String newPath = "${copyTo.path}/doggie_database.db";
            await source1.copy(newPath);

            setState(() {
              message = 'Successfully Copied DB';
            });
          },
          child: const Text('Copy DB'),
        ),
        
        ElevatedButton(
          onPressed: () async {
            var databasesPath = await getDatabasesPath();
            var dbPath = join(databasesPath, 'doggie_database.db');

            FilePickerResult? result =
                await FilePicker.platform.pickFiles();

            if (result != null) {
              File source = File(result.files.single.path!);
              await source.copy(dbPath);
              setState(() {
              message = 'Successfully Restored DB';
            });
            } else {
              // User canceled the picker

            }
          },
          child: const Text('Restore DB'),
        ),

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM