gpt4 book ai didi

swift : Trouble Exporting Database

转载 作者:行者123 更新时间:2023-11-30 10:56:57 29 4
gpt4 key购买 nike

我正在尝试导出我的数据库。我尝试过通过电子邮件并分享它。

问题是我想导出数据库的当前状态(以及其中的所有信息)。

我已经尝试过这段代码:

func exportDatabase(){
var url:String = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).last! as String
url = url + "/WalletDatabase.sqlite"

let fileManager = FileManager.default
if fileManager.fileExists(atPath: url) {
sendMail(sUrl: url)
}else{
print("error")
}
}

func sendMail(sUrl:String){
if( MFMailComposeViewController.canSendMail() ) {
print("Can send email.")

let fileManager = FileManager.default
let mailComposer = MFMailComposeViewController()
mailComposer.setToRecipients(["***@gmail.com"])
mailComposer.mailComposeDelegate = self

//Set the subject and message of the email
mailComposer.setSubject("Lorem Ipsum")
mailComposer.setMessageBody("Lorem Ipsum.", isHTML: false)

if let fileData = NSData(contentsOfFile: sUrl) {
print("File data loaded.")
mailComposer.addAttachmentData(fileData as Data, mimeType: "application/x-sqlite3", fileName: "WalletDatabase")
}
let fileData = fileManager.contents(atPath: sUrl)
} else {
print("error")
}

但是:

  • 发送的文件没有任何类型
  • 数据库为空,仅保留表和列

你们能给我一点帮助吗

最佳答案

我通过添加以下代码来修复它:

    static func migrateStoreSwift() -> NSURL
{
let lApp:AppController = UIApplication.shared.delegate as! AppController;
let lCurrentStore:NSPersistentStore = lApp.persistentStoreCoordinator.persistentStores.last!
let lNewDataBase = "Database.sqlite"
let lNewStoreURL:NSURL = lApp.applicationDocumentsDirectory()?.appendingPathComponent(lNewDataBase) as! NSURL
try! lApp.persistentStoreCoordinator.migratePersistentStore(lCurrentStore, to: lNewStoreURL as URL, options: nil, withType: NSSQLiteStoreType)
return lNewStoreURL
}

此代码创建数据库当前版本的副本。

关于 swift : Trouble Exporting Database,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53853094/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com