Flutter restore SQLite Db from Dropbox



我正在尝试为我的应用程序实现一个恢复数据库,以解决电话续订的问题。

我刚刚上传了我的数据库文件";database.db";到Dropbox,我设法再次将文件下载回来,但每当我在下载后尝试打开Db时,我都会收到一个异常,似乎是文件以某种方式损坏了。我必须将它保存到dropbox还是下载一些特定的参数?

E/SQLiteLog(16429): (26) file is not a database
D/SQLiteConnection(16429): Corruption detected - isPrimary: true, address: @2e80143
E/SQLiteDatabase(16429): Failed to open database '/data/user/0/com.testdropbox.test/app_flutter/database.db'.
E/SQLiteDatabase(16429): android.database.sqlite.SQLiteDatabaseCorruptException: file is not a database (code 26 SQLITE_NOTADB[26]): , while compiling: PRAGMA journal_mode
Future downloadTest() async {
if (await checkAuthorized(authorize: true)) {
Directory docsDirectory =
await pathProvider.getApplicationDocumentsDirectory();
String dbPath = join(docsDirectory.path, DB_NAME);
await _dbService.closeDb();
final result =
await Dropbox.download('/db/$DB_NAME', dbPath, (downloaded, total) {
print('progress $downloaded / $total');
});
print(result);
print(io.File(dbPath).statSync());
await _dbService.openDb();
}
}
Future uploadTest() async {
if (await checkAuthorized(authorize: true)) {
io.Directory docsDirectory =
await pathProvider.getApplicationDocumentsDirectory();
String filepath = join(docsDirectory.path, DB_NAME);
io.File(filepath).writeAsStringSync(
'contents.. from ' + (io.Platform.isIOS ? 'iOS' : 'Android') + 'n');
final result =
await Dropbox.upload(filepath, '/db/$DB_NAME', (uploaded, total) {
print('progress $uploaded / $total');
});
print(result);
var _content = await listFolder('');
print(_content);
}
}

好吧,我的建议是,我把变量和路径搞砸了,在dropbol上传之前,我正在写文本并覆盖数据库文件,这一行是:

io.File(filepath).writeAsStringSync(
'contents.. from ' + (io.Platform.isIOS ? 'iOS' : 'Android') + 'n');

最新更新