Exporting Sqlite table data to csv file programatically - xcode iOS 5 (for an ipad app)

后端 未结 1 1439
遇见更好的自我
遇见更好的自我 2021-01-23 03:45

I am developing a simple app. I am using sqlite to save data into a table (locally, in app documents folder). I want to problematically export this table data in a csv file and

1条回答
  •  南笙
    南笙 (楼主)
    2021-01-23 04:20

    Here what i have done to generate csv for tabular data. Here i have done it for simple testing. but what you need to do is to generate 2d array in such manner to produce NSArray like one i have done in this sample.

    Here first component in NSArray is one row for table , second component is for second row of table and likewise...

    NSArray *array = [NSArray arrayWithObjects:@"NAME,NUMBER",@"\nbhargavi1,12345",@"\nHiral,23456",@"\nHarish,34567", nil];    
    
    NSString *docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString *filename=[NSString stringWithFormat:@"test.csv"];
    NSString *filePathLib = [NSString stringWithFormat:@"%@",[docDir stringByAppendingPathComponent:filename]];
    
    [[array componentsJoinedByString:@","] writeToFile:filePathLib atomically:YES encoding:NSUTF8StringEncoding error:NULL];
    

    please implement this. May be this could help you out.

    i have implement this in my project and it works for me very nicely

    0 讨论(0)
提交回复
热议问题