1

Possible Duplicate:
How to share all the data i have stored using sqlite database between two devices to reuse the data by dumping in another device

I want to import a sqlite Database and save it as a CSV file and then mail this file via email.

can i import sqlite in to csv format in iphone app programmatically to attach in mail composer


i used following functio to get the data base file

+(NSString*)getSqliteDBFile
{
      NSString *docsDirectoryPath;
      NSArray *dirPaths;
      NSString *databasePath;
      NSFileManager *fileManager = [NSFileManager defaultManager];

      // Get the documents directory
      dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

      docsDirectoryPath = [dirPaths objectAtIndex:0];
      NSLog(@"path :%@", docsDirectoryPath);

      // Build the path to the database file
      databasePath = [[NSString alloc] initWithString: [docsDirectoryPath stringByAppendingPathComponent: @"LeadGenDB.sqlite"]];
      NSLog(@"path :%@", databasePath);


      return databasePath;
}

Now I nees to convert sqlite file to csv file

3
  • 1
    Have you tried anything? Commented Sep 11, 2012 at 13:01
  • It's certainly doable, but the whole sequence would be quite a bit of code. StackOverflow is not here to provide complete solutions. Commented Sep 11, 2012 at 13:07
  • That is just getting the path, you will need to query the database and then put the results into a CSV file <- not a complete solution but a good start. When you get stuck post a specific question. Commented Sep 11, 2012 at 13:15

1 Answer 1

3

you can not convert direct sqlite to csv. First you get your all database data and write into csv file.use below example for create csv and send it.

NSString *csvString =@"1,2 , 3 , 4, 5 ,6 , 7";

        for(int i=0;i<rangeData.count ;i++)
        {
            NSArray *temp = [di timeCalculation:[[rangeData objectAtIndex:i] objectForKey:@""] :[[rangeData objectAtIndex:i] objectForKey:@""]];



            NSString *strTemp =[NSString stringWithFormat:@"%@,%@,%@,%@,%@,%@,%@",

                                [NSString stringWithFormat:@"%@",[simpleDate stringFromDate:[[NSDate alloc]initWithTimeIntervalSince1970:[[[rangeData objectAtIndex:i] objectForKey:@""] floatValue]]]],
                                [NSString stringWithFormat:@"%@",[shrotDate stringFromDate:[[NSDate alloc]initWithTimeIntervalSince1970:[[[rangeData objectAtIndex:i] objectForKey:@""] floatValue]]]],
                                [NSString stringWithFormat:@"%@",[shrotDate stringFromDate:[[NSDate alloc]initWithTimeIntervalSince1970:[[[rangeData objectAtIndex:i] objectForKey:@""] floatValue]]]],
                                [NSString stringWithFormat:@"%@",[shrotDate stringFromDate:[[NSDate alloc]initWithTimeIntervalSince1970:[[[rangeData objectAtIndex:i] objectForKey:@""] floatValue]]]],
                                [NSString stringWithFormat:@"%@",[shrotDate stringFromDate:[[NSDate alloc]initWithTimeIntervalSince1970:[[[rangeData objectAtIndex:i] objectForKey:@""] floatValue]]]],

                                [NSString stringWithFormat:@"%@",[[temp objectAtIndex:0] stringByReplacingOccurrencesOfString:@": " withString:@""]] ,
                                [NSString stringWithFormat:@"%@",[[temp objectAtIndex:1] stringByReplacingOccurrencesOfString:@" : " withString:@""]]];
            csvString = [NSString stringWithFormat:@"%@ \n %@",csvString,strTemp];
        }

        //create instance of NSFileManager
        NSFileManager *fileManager = [NSFileManager defaultManager];

        //create an array and store result of our search for the documents directory in it
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

        //create NSString object, that holds our exact path to the documents directory
        NSString *documentsDirectory = [paths objectAtIndex:0];
        NSLog(@"Document Dir: %@",documentsDirectory);

        NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.csv", @"userdata"]]; //add our file to the path
        [fileManager createFileAtPath:fullPath contents:[csvString dataUsingEncoding:NSUTF8StringEncoding] attributes:nil];


        NSData *csvData =[NSData dataWithContentsOfFile:fullPath];
        MFMailComposeViewController* controller = [[MFMailComposeViewController alloc] init];
        controller.mailComposeDelegate = self;
        [controller setToRecipients:[NSArray arrayWithObject:@""]];
        [controller setSubject:@"CSV Export"];
        [controller setMessageBody:@"" isHTML:NO]; 
        [controller addAttachmentData:csvData mimeType:@"text/csv" fileName:@"userdata.csv"];
        [self presentModalViewController:controller animated:YES];
        [controller release];
Sign up to request clarification or add additional context in comments.

2 Comments

here rangedata is the data array retrieved from atable in the my database right?
yes you use your own array for that.it is my demo example.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.