通过电子邮件在文档目录中导出 SQLite 文件
Posted
技术标签:
【中文标题】通过电子邮件在文档目录中导出 SQLite 文件【英文标题】:Export SQLite file in document directory via email 【发布时间】:2015-02-25 05:29:37 【问题描述】:当用户点击 exportAllData 按钮时,我正在尝试通过位于文档目录中的电子邮件导出 SQLite 文件。使用下面的代码,我能够打开邮件应用程序并附加一个文件。但是,当我发送它时,文件不会随电子邮件一起发送。
- (IBAction)exportAllDataButtonPressed:(id)sender
MFMailComposeViewController *composer=[[MFMailComposeViewController alloc]init];
composer.mailComposeDelegate = self;
if ([MFMailComposeViewController canSendMail])
[composer setToRecipients:[NSArray arrayWithObjects:@"test@test.com",nil]];
[composer setSubject:@"SQLite File"];
[composer setMessageBody:@"This is your SQLite file" ishtml:NO];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES);
NSString *documentDirectory = [paths objectAtIndex:0];
NSString *path = [documentDirectory stringByAppendingPathComponent:@"mooddb.sql"];
NSData *myData = [NSData dataWithContentsOfFile:path];
[composer addAttachmentData:myData mimeType:@"application/x-sqlite3" fileName:path];
[self presentViewController:composer animated:YES completion:nil];
【问题讨论】:
请检查我的数据,当您发送时可能是 nil...??? 它工作了吗?我得到空数据。请在此处查看我的问题***.com/q/56151110/1635315。 【参考方案1】:[composer addAttachmentData:myData mimeType:@"application/x-sqlite3" fileName:path];
在 FileName 中,您正在发送路径... 所以请检查文件名的路径.. 喜欢..
[composer addAttachmentData:myData mimeType:@"application/x-sqlite3" fileName:@"mooddb.sql"];
【讨论】:
这只是改变文件名。【参考方案2】:原来我在错误的目录中搜索。
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES);
应该是:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
【讨论】:
【参考方案3】:Swift 版本
@IBAction func exportAllDataButtonTapped(_ sender: Any)
guard MFMailComposeViewController.canSendMail() == true else return
let mailCompose = MFMailComposeViewController()
mailCompose.mailComposeDelegate = self
mailCompose.setSubject("Sqlite File")
mailCompose.setMessageBody("", isHTML: false)
if let documentsPathURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first
let fileName = "myDatabase.sqlite"
let fullURL = documentsPathURL.appendingPathComponent(fileName)
if let data = try? Data(contentsOf: fullURL)
mailCompose.addAttachmentData(data, mimeType: "application/x-sqlite3", fileName: fileName)
present(mailCompose, animated: true, completion: nil)
【讨论】:
以上是关于通过电子邮件在文档目录中导出 SQLite 文件的主要内容,如果未能解决你的问题,请参考以下文章
如何通过 DataGrip IDE 在单个文件中导出和导入 MySQL 模式