从 Google 驱动器 swift 下载 Sqlite 文件

Download Sqlite file from Google Drive swift

我将 sqlite 数据库上传到 google 驱动器。现在我想下载它。所以我尝试将它从 Google 驱动器下载到我的 Swift 项目,但它是以 GTLRDataObject 数据格式下载的。如何将文件转换为 sqlite 数据库格式? sqlite 文件来自 Google 驱动器的数据格式。我使用以下命令从 google 驱动器下载我的 sqlite 数据库文件。

Global.googleDriveService.executeQuery(GTLRDriveQuery_FilesGet.queryForMedia(withFileId: "\((result as? GTLRDrive_File)?.identifier ?? "")"))
{ (ticket, file, error) in
    guard let data = (file as? GTLRDataObject)?.data else {
        return
    }
    print("Download data: - \(data)")
}

如果你这样做是为了备份你的数据库和恢复你的数据库文件,那么你需要像下面那样做

  func getAndSaveFileromGoogle() {
    let query = GTLRDriveQuery_FilesList.query()
    query.spaces = "drive"
    self.googleDriveService.executeQuery(query) { (ticket, files, error) in
        if error == nil {
            if let files = files as? GTLRDrive_FileList {
                if let driveFiles = files.files /*?? [GTLRDrive_File]()*/ {
                    if driveFiles.count > 0 {
                        for file in driveFiles {
                            if file.name == "your_filename.sqlite" {
                                print(file.name)
                                print(file.identifier)
                                let downloadQuery = GTLRDriveQuery_FilesGet.queryForMedia(withFileId: file.identifier!)
                                self.googleDriveService.executeQuery(downloadQuery, completionHandler: { (ticket, downloadedFile, error) in
                                    if error == nil {
                                        if let downlaodfile = downloadedFile as? GTLRDataObject {
                                            do {
                                                try downlaodfile.data.write(to: Model.shared.coreDataStoreURL!, options: .atomic)
                                            }
                                            catch {
                                                print(error.localizedDescription)
                                            }
                                        }
                                    }
                                    else {
                                        print("Somethig went wrong")
                                    }
                                })
                            }
                        }
                    }
                    else {
                        
                        print("No back up file found")
                    }
                }
                else {
                    
                    print("No back up file found")
                }
            }
            else {
                
                print("Something went wrong")
            }
        }
        else {
            print("Something went wrong")
        }
    }
}

只需将您的数据库文件替换为新下载的文件即可。