Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/115.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 从Google Drive swift下载Sqlite文件_Ios_Swift_Sqlite_Local Database - Fatal编程技术网

Ios 从Google Drive swift下载Sqlite文件

Ios 从Google Drive swift下载Sqlite文件,ios,swift,sqlite,local-database,Ios,Swift,Sqlite,Local Database,我上传了一个sqlite数据库到GoogleDrive。现在我想下载它。所以我试着把它从谷歌硬盘下载到我的Swift项目中,但它是以GTLRDataObject数据格式下载的。如何将文件转换为sqlite数据库格式?sqlite文件以数据格式来自Google drive。我使用以下命令从GoogleDrive下载我的sqlite数据库文件 Global.googleDriveService.executeQuery(GTLRDriveQuery_FilesGet.queryForMedia(wi

我上传了一个sqlite数据库到GoogleDrive。现在我想下载它。所以我试着把它从谷歌硬盘下载到我的Swift项目中,但它是以GTLRDataObject数据格式下载的。如何将文件转换为sqlite数据库格式?sqlite文件以数据格式来自Google drive。我使用以下命令从GoogleDrive下载我的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")
        }
    }
}

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

您是否将文件以sqlite格式存储在GD上?我的意思是您的文件mimeType是“application/x-sqlite3I used mimeType”db/SQLite“检查我编辑的答案,它的工作代码确定谢谢,但你能建议我,当我在新设备检查,我得到文件夹id。但要下载那个文件,我还需要文件id,那个么我怎样才能得到这个文件id呢?非常感谢你们的建议,我的建议真的很有效。但现在我想问我的数据库是否保存在文档目录中。下载此文件后,我想替换该数据库文件,以便在代码行下面传入哪个url:尝试downlaodfile.data.write(to:Model.shared.coreDataStoreURL!,options:.atomic)好的,我知道了,您的问题可以告诉我您使用的是哪个数据库吗?
if driveFiles.count>0{
如果没有文件,它无论如何都不会迭代循环。我使用的是使用FMDB的Sqlite数据库。