我上传了一个sqlite数据库到谷歌驱动器。现在我想下载它。所以我试着从谷歌硬盘下载到我的Swift项目,但它是以GTLRDataObject数据格式下载的。如何将文件转换为sqlite数据库格式?sqlite文件是来自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")
}
}
}
只需将数据库文件替换为新下载的文件。