使用FMDB和CHCSVWriter将列标题导出到csv

时间:2015-05-04 16:56:46

标签: objective-c sqlite csv fmdb

我创建了一个应用程序,我使用FMDB将数据存储在SQLite数据库中。

我使用CHCSVWriter将一些表从db导出到csv。

导出列标题行的最佳方法是什么? 我应该使用' orderedKeys'并写下'columnName'先转到写内容?

代码

FMDatabase *db = [FMDatabase databaseWithPath:[Utility getDatabasePath]];
[db open];
FMResultSet *results = [db executeQuery:@"SELECT * FROM tblScores"];

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder
NSString *document = [[NSString alloc] initWithFormat:@"%@/demo.csv", documentsDirectory];

CHCSVWriter *csvWriter = [[CHCSVWriter alloc] initForWritingToCSVFile:document];

while([results next]) {
    NSDictionary *resultRow = [results resultDictionary];
    NSArray *orderedKeys = [[resultRow allKeys] sortedArrayUsingSelector:@selector(compare:)];
    //iterate over the dictionary
    for (NSString *columnName in orderedKeys) {
        id value = [resultRow objectForKey:columnName];
        [csvWriter writeField:value];
    }
    [csvWriter finishLine];
}
[csvWriter closeStream];

0 个答案:

没有答案