首页 > 解决方案 > Flutter 从 Dropbox 恢复 SQLite Db

问题描述

我正在尝试为我的应用程序实现一个还原数据库,以涵盖电话续订的情况。

我刚刚将我的 db 文件“database.db”上传到 Dropbox,我设法再次下载该文件,但是每当我在下载后尝试打开 Db 时,我都会遇到异常,并且似乎文件以某种方式被损坏了. 我是否必须将其保存到保管箱或使用某些特定参数下载?

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);
    }
  }

标签: sqliteflutter

解决方案


好的,我对此表示歉意,我用路径弄乱了变量,并且在这一行中的 dropbol 上传之前写入了文本并覆盖了数据库文件:

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

推荐阅读