[英]Export sqlite DB as csv - Sqlite.swift
I am using sqlite in my iOS application. 我在iOS应用程序中使用sqlite。 I would like to export this db and put it in a centralized db.
我想导出此数据库并将其放在集中式数据库中。 How to export sqlite db?
如何导出sqlite数据库? I am using sqlite.swift.
我正在使用sqlite.swift。 Thanks!
谢谢!
this following is only a portion of the code. 以下只是代码的一部分。 Pasted below is what is required to answer your question:
下面粘贴的是回答您的问题所需要的:
- (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];
}
This line of code, NSArray *arrayContents = [self SQL_returnContentsOfTable];
这行代码是
NSArray *arrayContents = [self SQL_returnContentsOfTable];
, returns the contents of the SQL Table. ,返回SQL表的内容。
SELECT * FROM Items;
was the query used to populate this array, arrayContents
是用于填充此数组的查询,
arrayContents
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.