Search code examples
iosswiftsqlitelocal-database

Download Sqlite file from Google Drive swift


I upload a sqlite database to google drive. And now I would like to download it. So I tried to download it from Google Drive to my Swift Project but it is being downloaded in GTLRDataObject Data format. How can I get the file into sqlite database format? The sqlite files comes as Data format from Google drive. I used the following to download my sqlite database file from google drive.

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

Solution

  • If you doing this for purpose of backup your database and restore your database file then you need to do like below

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

    Just replace your database file to new downloaded file.