【发布时间】:2021-09-14 03:12:37
【问题描述】:
我想将我的 Flutter SQFlite 数据库备份到移动设备。 我需要在现有应用程序中恢复数据库。 我在互联网上搜索,但没有得到任何合适的教程或文档。 任何人请帮助! 提前致谢。
【问题讨论】:
标签: database flutter backup restore sqflite
我想将我的 Flutter SQFlite 数据库备份到移动设备。 我需要在现有应用程序中恢复数据库。 我在互联网上搜索,但没有得到任何合适的教程或文档。 任何人请帮助! 提前致谢。
【问题讨论】:
标签: database flutter backup restore sqflite
如需完整详情,请转至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'),
),
【讨论】: