2

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

2
  • When you say its not working, what is not working? What happens, errors, logs? Commented Aug 16, 2012 at 17:44
  • // TODO: mutex lock? [sqliteDb exportCsv: csvPath]; -This is the place I dont understand... So I used the documentpath instead... do you think the code is a working code? Do you have any suggestions please? Commented Aug 16, 2012 at 18:10

1 Answer 1

5

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

Sign up to request clarification or add additional context in comments.

2 Comments

If phone number is 10 digit then it gives different format. Also I want to have new line in a column. It will be like I have two phone numbers, then In phone column it will be on two lines but in same row.
@Durgaprasad I have not tried new line in same column, if you find solution for same then post here so that others can review same.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.