0

I am using sqlite in my iOS application. I would like to export this db and put it in a centralized db. How to export sqlite db? I am using sqlite.swift. Thanks!

  • possible duplicate of [How do I dump the data of some SQLite3 tables?](http://stackoverflow.com/questions/75675/how-do-i-dump-the-data-of-some-sqlite3-tables) – beresfordt Jul 12 '15 at 16:39
  • it's not a duplicate coz i need to do it in-app. – tryinghardladyprogrammer Jul 12 '15 at 17:59
  • oooh snap! ;3 so you just want to do exactly what happens in the duplication shown above by @beresfordt but save this file in-app say in the documents folder of the app? – ErickES7 Jul 14 '15 at 20:50

1 Answers1

0

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];


}

example

This line of code, NSArray *arrayContents = [self SQL_returnContentsOfTable];, returns the contents of the SQL Table. SELECT * FROM Items; was the query used to populate this array, arrayContents

ErickES7
  • 558
  • 6
  • 16
  • if you'd like any explanation, because i just copied a pasted my code, please i'll be happy to help :) – ErickES7 Jul 14 '15 at 21:17