Flutter: 如何从 Google Drive 恢复或下载 SQLite 数据库

huangapple go评论60阅读模式
英文:

Flutter: How to restore or download sqlite database from google drive

问题

我正在开发一个使用SQLite数据库的Flutter应用程序。用户可以备份数据库并将其保存到Google Drive,这部分功能正常。然而,我在尝试恢复备份时遇到了困难,尝试了许多方法,但每次都失败。

Future<void> _downloadFile(String fileName, String fileId) async {
    final googleSignIn = sign_in.GoogleSignIn.standard(scopes: [drive.DriveApi.driveScope]);
    final sign_in.GoogleSignInAccount? account = await googleSignIn.signIn();
    final authheader = await account?.authHeaders;

    var client = GoogleAuthClient(authheader!);
    var driveFile = drive.DriveApi(client);

    var databasesPath = await getDatabasesPath();
    var dbPath = p.join(databasesPath, 'my_database.db');
    
    // 我不确定接下来该怎么做。我需要从Google Drive中检索文件并用my_database.db替换它。
}
英文:

I am developing a Flutter application that uses an SQLite database. The user can take a backup of the database and save it to Google Drive, which works fine. However, I am having trouble figuring out how to restore the backup. I have tried many ways, but I have failed each time.

Future<void> _downloadFile(String fileName, String fileId) async {
    final googleSignIn = sign_in.GoogleSignIn.standard(scopes: [drive.DriveApi.driveScope]);
    final sign_in.GoogleSignInAccount? account = await googleSignIn.signIn();
    final authheader = await account?.authHeaders;

    var client = GoogleAuthClient(authheader!);
    var driveFile = drive.DriveApi(client);

    var databasesPath = await getDatabasesPath();
    var dbPath = p.join(databasesPath, 'my_database.db');
    
    // I'm not sure what to do next. I need to retrieve the file from Google Drive and replace it with my_database.db.
}

答案1

得分: 0

这个解决方案对我有效,除非我在数据库中有视图,否则它不会生效,除非我将文件下载到本地设备并进行恢复。
尝试找一种方法,允许您将数据库下载为文件到本地设备。因此,使用此代码直到完成。

编辑 我已经找到了一种下载文件然后复制到数据库路径的方法,实际上与上面的代码相同,但有一些小的更改。

英文:

This solution works for me, except if I have view in database it won't affect until I restart the app, unless I download it the file to local device and the restore it.
try to find away that let you download the db as a file to your local device. So use this code until then.

  Future<void> _downloadFile(String fileName, String fileId) async {
    final googleSignIn =
        sign_in.GoogleSignIn.standard(scopes: [drive.DriveApi.driveScope]);
    final sign_in.GoogleSignInAccount? account = await googleSignIn.signIn();
    final authheader = await account?.authHeaders;

    var client = GoogleAuthClient(authheader!);
    var driveFile = drive.DriveApi(client);

    //here you go:::::::::::::::::

    var result = (await driveFile.files
        .get(fileId, downloadOptions: drive.DownloadOptions.fullMedia)) ;
    if (result is! drive.Media) throw Exception("invalid response");
    var databasesPath = await getDatabasesPath();
    var dbPath = p.join(databasesPath, 'my_database.db');
    final saveFile = File(dbPath);
    print('dbPath $dbPath');
    List<int> dataStore = [];
    result.stream.listen((event) {
      print('data received : ${event.length}');
      dataStore.insertAll(dataStore.length, event);
    }, onDone: () {
      print('task done');
      saveFile.writeAsBytes(dataStore);
      print("File saved at ${saveFile.path}");
    }, onError: (error) {
      print("Some Error");
    });
  }

Edit I have found a way to download the file and then copy it to database path, its actually same above code but with little Changs:

  Future<void> _downloadFile(String fileName, String fileId) async {
    final googleSignIn =
        sign_in.GoogleSignIn.standard(scopes: [drive.DriveApi.driveScope]);
    final sign_in.GoogleSignInAccount? account = await googleSignIn.signIn();
    final authheader = await account?.authHeaders;

var client = GoogleAuthClient(authheader!);
    var driveFile = drive.DriveApi(client);

    var result = (await driveFile.files
        .get(fileId, downloadOptions: drive.DownloadOptions.fullMedia));
    if (result is! drive.Media) throw Exception("invalid response");
    var databasesPath = await getDatabasesPath();
    var dbPath = p.join(databasesPath, 'my_database.db');
    print('dbPath $dbPath');

    Directory copyTo = Directory(createAppFolders.dbAppDir!);
    print('copyTo: $copyTo');
    File newPath = File("${copyTo.path}/my_database.db");

    final saveFile = newPath;

    List<int> dataStore = [];
    result.stream.listen((event) {
      print('data received : ${event.length}');
      dataStore.insertAll(dataStore.length, event);
    }, onDone: () async {
      await saveFile.writeAsBytes(dataStore).then((value) => newPath.copy(dbPath));
      print('task done');
      print("File saved at ${saveFile.path}");
    }, onError: (error) {
      print("Some Error");
    });
  }

huangapple
  • 本文由 发表于 2023年6月16日 13:05:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/76487092.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定