我正在尝试使用 flutter 备份和恢复 sqlite 数据库。数据库文件通过php api备份到服务器上;恢复也一样。
返回错误 未处理的异常:DatabaseException(错误域= FMDatabase代码= 26“文件不是数据库”UserInfo = {NSLocalizedDescription =文件不是数据库})sql'SELECT * FROM Journal'args []
这是我在服务器上备份和在应用程序中恢复的功能
备份功能
static exportDB(bool isWithMessage) async {
if(int.parse(Common.storage.read('user')['abonnement'])==1)
{
String path = await getDatabasesPath();
path += '/jonadatabase';
// Create an instance of GetConnect
final GetConnect _connect = GetConnect(
timeout: const Duration(seconds: 10),);
final FormData _formData = FormData({
'fichier': MultipartFile(File(path), filename: 'jonadatabase'),
'user_id': Common.storage.read('user')['id']
});
Map<String, String> headers = {
'Authorization': 'Bearer ${Common.storage.read('user')['token']}'
};
try {
final Response res = await _connect.post(
'${Common.apiLink}/fichier/${Common.storage.read('user')['id']}',
_formData, headers: headers);
if (res.body['status'].toString() == 'true') {
if (isWithMessage == true) {
Get.back();
Common.customSnackbar(
'Info', "DBExportSuccessfully".tr, Icons.check_circle,
CustomColor.primaryColor);
}
} else {
Get.back();
Common.customSnackbar(
'error'.tr, "errorWhenExportDB".tr, Icons.check_circle,
CustomColor.primaryColor);
}
} catch (err) {
// Handle errors
print(err);
}
}
else
{
if(isWithMessage == true) {
Common.customSnackbar(
'error'.tr, "upgradeToEverywherePlan".tr, Icons.info,
CustomColor.primaryColor, sec: 7);
Get.defaultDialog(title: '', content: Pricing(type: 'renew'));
}
}}
恢复功能
static loadDB() async {
var url = "${Common.webLink}/uploads/database/";
var filename = 'copy';
await Common.downloadFile(url, filename);
var dbPath = join(await getDatabasesPath(),'jonadatabase');
ByteData data = await rootBundle.load(join(await getDatabasesPath(), 'copy'));
List<int> bytes = data.buffer.asUint8List(data.offsetInBytes, data.lengthInBytes);
await File(dbPath).writeAsBytes(bytes);
MainController.to.database = await openDatabase(dbPath);
if(Common.storage.hasData('user')) {
Common.storage.read('user')['fichier'] = 'copy';
}
}
这个问题你解决了吗?我也有同样的问题