2

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)")
}
jamoreiras
  • 315
  • 1
  • 14
Maulik Patel
  • 23
  • 1
  • 4

1 Answers1

1

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.

Ruchi Makadia
  • 1,005
  • 1
  • 7
  • 20
  • Ok thanks, but can you suggest me, When I check in new device and I got folder id . But to download that file I also need File id for that, so how can I get this file id? – Maulik Patel Jul 18 '20 at 05:28
  • Thank you so so much for this suggestion it really works from me. But now I want to ask that my database was saved in document directory . so after download this file I want to replace that database file so which url should need to pass in below line of code: try downlaodfile.data.write(to: Model.shared.coreDataStoreURL!, options: .atomic) – Maulik Patel Jul 18 '20 at 06:14
  • ok i got it your question can you tell me which database are you use? – Ruchi Makadia Jul 18 '20 at 06:21
  • 1
    `if driveFiles.count > 0 {` if there is no file it will not iterate the loop anyway – Leo Dabus Jul 18 '20 at 06:32
  • I am using Sqlite database using FMDB. – Maulik Patel Jul 18 '20 at 06:45
  • actually i have no idea about that but i would suggest that when you put file in gd at that time you get sqlite file using url right ? then this is a same path where sqlite file create so store that url – Ruchi Makadia Jul 18 '20 at 08:32
  • Ok I solved it . But again Thank you so so so much for your valuable suggestion. – Maulik Patel Jul 18 '20 at 08:44
  • If I upload my DB file into drive it upload twice so in Drive it generates two file in folder.how to do if we want to upload file but it shows only one? Ruchi Makadia madam if you will give this Answer then it would be great for me !! – Maulik Patel Jul 18 '20 at 09:21
  • can you create new question with your code which you try coz without showing your code it's hard for me to answer so i request to create new question. – Ruchi Makadia Jul 18 '20 at 10:02