1

可能重复:
如何在两台设备之间共享我使用 sqlite 数据库存储的所有数据,以通过转储到另一台设备来重用数据

我想导入一个 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 文件

4

1 回答 1

3

您不能将 sqlite 直接转换为 csv。首先,您获取所有数据库数据并写入 csv 文件。使用下面的示例创建 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];
于 2012-09-11T13:17:29.393 回答