将sqlite数据库导出为csv - Sqlite.swift
问题描述:
我在我的iOS应用程序中使用sqlite。我想导出这个数据库,并把它放在一个集中的数据库。如何导出sqlite数据库?我正在使用sqlite.swift。谢谢!将sqlite数据库导出为csv - Sqlite.swift
答
此内容仅为代码的一部分。下面粘贴的是需要什么来回答你的问题:
- (NSString *)dataFilePathCustom:(NSString *)filename withExtension:(NSString *)ext {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *dictionaryDocuments = [paths objectAtIndex: 0];
return [dictionaryDocuments stringByAppendingPathComponent: [NSString stringWithFormat: @"%@.%@", filename, ext]];
}
- (void)SQL_ITEMS_voidExportCSVFormat {
NSString *string = @"\"CSV Export\"";
NSArray *arrayContents = [self SQL_returnContentsOfTable];
for (NSArray *arrayItem in arrayContents) {
string = [string stringByAppendingFormat: @"\r%d,\"%@\",%d", [arrayItem[0] intValue], arrayItem[1], [arrayItem[2] intValue]];
}
[string writeToFile: [self dataFilePathCustom: @"ExportFile" withExtension: @"csv"] atomically: YES encoding: NSUTF8StringEncoding error: nil];
}
- (void)viewDidLoad {
[self SQL_voidClearRowsFromTable];
[self SQL_ITEMS_voidInsertRowWithArray: @[@"ONE", @25]];
[self SQL_ITEMS_voidInsertRowWithArray: @[@"TWO", @225]];
[self SQL_ITEMS_voidInsertRowWithArray: @[@"THREE", @21]];
[self SQL_ITEMS_voidInsertRowWithArray: @[@"FOUR", @55]];
[self SQL_ITEMS_voidInsertRowWithArray: @[@"FIVE", @56]];
[self SQL_ITEMS_voidInsertRowWithArray: @[@"SIX", @77]];
[self SQL_ITEMS_voidInsertRowWithArray: @[@"SEVEN", @25]];
[self SQL_ITEMS_voidExportCSVFormat];
}
这行代码,NSArray *arrayContents = [self SQL_returnContentsOfTable];
,返回SQL表的内容。是用于填充此数组的查询,arrayContents
+0
如果你想要任何解释,因为我刚刚复制粘贴我的代码,请我很乐意帮助:) – ErickES7
可能的重复[如何转储某些SQLite3表的数据?](http://stackoverflow.com/questions/75675/how-do-i-dump -sqlite3-表的数据) – beresfordt
它不是重复的因为我需要在应用程序中做它。 – tryinghardladyprogrammer
oooh snap! ; 3所以你只是想完成上面显示的@beresfordt所做的重复操作,但是将这个文件保存在应用程序的文档文件夹中? – ErickES7