【问题标题】:Backup and Restore Flutter SQFlite database备份和恢复 Flutter SQFlite 数据库
【发布时间】:2021-09-14 03:12:37
【问题描述】:

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

【问题讨论】:

    标签: database flutter backup restore sqflite


    【解决方案1】:

    如需完整详情,请转至link

    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'),
            ),
    

    【讨论】:

    • 当链接到您自己的网站或内容(或您附属的内容)时,您must disclose your affiliation in the answer 以免被视为垃圾邮件。根据 Stack Exchange 政策,在您的用户名中包含与 URL 相同的文本或在您的个人资料中提及它不被视为充分披露。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-08-14
    • 1970-01-01
    • 1970-01-01
    • 2023-03-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多