1

我正在从 sqlite 数据库读取信息到 .csv 文件。我能够构建文件,但每个新行都以“,”开头。我做了一些android编程,但没有太多ios。我认为问题是 componentsJoinedByString:@"," 因为它将所有内容连接在一起,但我不知道如何在每个新行的开头使用它来摆脱 ","。是否有另一种方法可以加入他们写入 csv 文件或摆脱第一个逗号的方法?

NSMutableArray *exportBike = [[NSMutableArray alloc] init];

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

NSString *docsPath = [paths objectAtIndex:0];

NSString *path = [docsPath stringByAppendingPathComponent:@"MemberDB.sql"];

FMDatabase *database = [FMDatabase databaseWithPath:path];

[database open];

FMResultSet *resultsBike = [database executeQuery:@"SELECT * FROM bike"];

while([resultsBike next]) {

    NSString *name = [resultsBike stringForColumn:@"name"];
    NSInteger stage1HR  = [resultsBike intForColumn:@"stage1HR"];
    NSInteger stage1BP = [resultsBike intForColumn:@"stage1BP"];
    NSInteger stage2HR = [resultsBike intForColumn:@"stage2HR"];
    NSInteger stage2BP = [resultsBike intForColumn:@"stage2BP"];
    NSInteger stage3HR  = [resultsBike intForColumn:@"stage3HR"];
    NSInteger stage3BP = [resultsBike intForColumn:@"stage3BP"];
    NSInteger stage4HR  = [resultsBike intForColumn:@"stage4HR"];
    NSInteger stage4BP = [resultsBike intForColumn:@"stage4BP"];
    NSInteger stage5HR  = [resultsBike intForColumn:@"stage5HR"];
    NSInteger stage5BP = [resultsBike intForColumn:@"stage5BP"];
    NSInteger stage6HR  = [resultsBike intForColumn:@"stage6HR"];
    NSInteger stage6BP = [resultsBike intForColumn:@"stage6BP"];
    NSInteger stage7HR  = [resultsBike intForColumn:@"stage7HR"];
    NSInteger stage7BP = [resultsBike intForColumn:@"stage7BP"];
    NSInteger recoveryHR = [resultsBike intForColumn:@"recoveryHR"];

    NSLog(@"%@,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d",name, stage1HR, stage1BP, stage2HR, stage2BP, stage3HR, stage3BP, stage4HR, stage4BP, stage5HR, stage5BP, stage6HR, stage6BP, stage7HR, stage7BP, recoveryHR);

    [exportBike addObject:[NSString stringWithFormat:@"%@,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d""\n",name, stage1HR, stage1BP, stage2HR, stage2BP, stage3HR, stage3BP, stage4HR, stage4BP, stage5HR, stage5BP, stage6HR, stage6BP, stage7HR, stage7BP, recoveryHR]];

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

    NSString *savePath = [paths objectAtIndex:0];

    savePath = [savePath stringByAppendingPathComponent:@"bike.csv"];

    [[exportBike componentsJoinedByString:@","] writeToFile:savePath atomically:YES encoding:NSUTF8StringEncoding error:NULL];
4

1 回答 1

3

改变

[exportBike addObject:[NSString stringWithFormat:@"%@,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d""\n",name, stage1HR, stage1BP, stage2HR, stage2BP, stage3HR, stage3BP, stage4HR, stage4BP, stage5HR, stage5BP, stage6HR, stage6BP, stage7HR, stage7BP, recoveryHR]];

[[exportBike componentsJoinedByString:@","] writeToFile:savePath atomically:YES encoding:NSUTF8StringEncoding error:NULL];

[exportBike addObject:[NSString stringWithFormat:@"%@,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d",name, stage1HR, stage1BP, stage2HR, stage2BP, stage3HR, stage3BP, stage4HR, stage4BP, stage5HR, stage5BP, stage6HR, stage6BP, stage7HR, stage7BP, recoveryHR]];

[[exportBike componentsJoinedByString:@"\n"] writeToFile:savePath atomically:YES encoding:NSUTF8StringEncoding error:NULL];

而不是在方法中","使用并在行中删除末尾的。"\n"componentsJoinedByStringaddObject"\n"

正如 Martin 在下面提到的,另一种选择是将换行符保留在addObjectline 中并使用[exportBike componentsJoinedByString:@""]. 这将在文件末尾添加一个换行符。

于 2013-01-21T20:07:44.813 回答