我正在将信息从sqlite数据库读取到.csv文件中。我能够获得要构建的文件,但是每个新行都以",“开头。我做过一些android编程,但ios不多。我认为问题是componentsJoinedByString:@",“因为它将所有东西连接在一起,但我不确定如何消除使用this的每个新行开头的",”。有没有其他方法可以将它们连接到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];发布于 2013-01-22 04:07:45
变化
[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];在componentsJoinedByString方法中使用"\n"而不是",",并在addObject行中删除末尾的"\n"。
正如马丁在下面提到的,另一种选择是将换行符保留在addObject行中,并使用[exportBike componentsJoinedByString:@""]。这将在文件的末尾添加一个新行字符。
https://stackoverflow.com/questions/14445113
复制相似问题