英文:
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");
});
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论