隐藏sqlite数据库到csv格式?然后在iPhone Objective C中邮寄此csv文件

时间:2012-09-11 12:54:50

标签: iphone objective-c sqlite csv

  

可能重复:
  How to share all the data i have stored using sqlite database between two devices to reuse the data by dumping in another device

我想导入sqlite数据库并将其另存为CSV文件 then mail this file via email.

可以在iphone应用程序中以编程方式将 sqlite导入到csv 格式以附加到邮件编辑器中


我使用以下函数来获取数据库文件

+(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;
}

现在我需要将sqlite文件转换为csv文件

1 个答案:

答案 0 :(得分:3)

你无法将直接sqlite转换为csv。首先,获取所有数据库数据并写入csv file.use以下示例中的create csv并发送它。

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