user1603847
user1603847

Reputation:

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

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 email it to a person. Export SQLite data to Excel in iOS programmatically - I tried this, but its not working... Can anybody give an example application to download? Or the code?

Currently, I am using these commands in command line to convert a db table to csv - .mode, .output out.csv, select * from table;

Please help

Upvotes: 2

Views: 6761

Answers (1)

βhargavḯ
βhargavḯ

Reputation: 9836

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

Upvotes: 5

Related Questions