Last active
August 29, 2015 14:20
-
-
Save AlexHedley/4ac103b5018c9d5fa974 to your computer and use it in GitHub Desktop.
Export SQLite to CSV
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| FMDatabase *db = [FMDatabase databaseWithPath:[Utility getDatabasePath]]; | |
| [db open]; | |
| FMResultSet *results = [db executeQuery:@"SELECT * FROM tblScores"]; | |
| //CHCSVWriter *csvWriter = [[CHCSVWriter alloc] initForWritingToCSVFile:[NSHomeDirectory() stringByAppendingPathComponent:@"demo.csv"]]; | |
| 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 resultDict]; | |
| 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 writeLine]; | |
| [csvWriter finishLine]; | |
| } | |
| //[csvWriter closeFile]; | |
| [csvWriter closeStream]; |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://github.com/davedelong/CHCSVParser
http://stackoverflow.com/questions/4656887/how-to-export-sqlite-file-into-csv-file-in-iphone-sdk