写入文本文件(追加)时,我写的最后一个值将替换所有以前的值

时间:2015-08-19 09:34:04

标签: ios objective-c xcode sqlite nsfilemanager

我正在尝试将我的SQLite3表重现为保存在手机文档中的.csv文件。

为了做到这一点,我试图在文件的末尾添加一个新行,每次在我的数组中找到一个对象时,这都有效 - 但是它似乎一遍又一遍地重现最后一行我的SQL表中的确切行数。

以下是SQLite Select查询:

    NSMutableArray *allGPS = [[NSMutableArray alloc] init];
    GPS *gps = [[GPS alloc] init];
// Open the database from the users filessytem
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *databasePath = [documentsDirectory stringByAppendingPathComponent:@"GPS.db"];
    if (sqlite3_open([databasePath UTF8String], &databaseHandle) == SQLITE_OK)
    {
        NSString *sqlStatement = [NSString stringWithFormat:@"SELECT * FROM GPSJob;"];
        sqlite3_stmt *statement;
        if(sqlite3_prepare_v2(databaseHandle, [sqlStatement UTF8String], -1, &statement, NULL) == SQLITE_OK) {
            NSLog(@"Statement prepared");
            while(sqlite3_step(statement) == SQLITE_ROW) 
            {
                [gps setJobNo:[NSString stringWithUTF8String:(char *)sqlite3_column_text(statement,0)]];
                [gps setSourceMonitor:[NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 1)]];
                [gps setPositionNo:[NSNumber numberWithInt:sqlite3_column_int(statement,2)]];
                [gps setLatitude:sqlite3_column_double(statement, 3)];
                [gps setLongitude:sqlite3_column_double(statement, 4)];
                NSLog(@"Source Monitor: %@", [gps sourceMonitor]);
                [allGPS addObject:gps];
            }
        }
                    sqlite3_finalize(statement);
    }

这是写.csv文件方法:

 GPS *saveGPS = [[GPS alloc] init];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *filePath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"/%@.csv", jobNo]];
    [[NSFileManager defaultManager] createFileAtPath:filePath contents:nil attributes:nil];
    NSFileHandle *myHandle = [NSFileHandle fileHandleForWritingAtPath:filePath];
    DataController *dataController = [[DataController alloc] init];
    NSString *csvTitleString =@"Source/Monitor, PositionNumber, Latitude, Longitude \n";
    [myHandle seekToEndOfFile];
    [myHandle writeData:[csvTitleString dataUsingEncoding:NSUTF8StringEncoding]];
    NSString *csvString = [[NSString alloc] init];
    NSArray *allGPS = [[NSArray alloc]initWithArray:[dataController getAll]];
    for(int i=0;i<(allGPS.count);i++){
        saveGPS = [allGPS objectAtIndex:i];
        csvString = [NSString stringWithFormat:@"%@, %d, %f, %f \n", [saveGPS sourceMonitor], [[saveGPS positionNo] intValue], [saveGPS latitude], [saveGPS longitude]];
        [myHandle seekToEndOfFile];
        [myHandle writeData:[csvString dataUsingEncoding:NSUTF8StringEncoding]];

2 个答案:

答案 0 :(得分:9)

您在重复循环中始终使用GPS的相同实例,因此,由于引用语义,对象中的数据将被覆盖,并且在每次迭代中将相同的对象添加到数组中。

移动线

GPS *gps = [[GPS alloc] init];

在行

之前
[gps setJobNo:[NSString stringWithUTF8String:(char *)sqlite3_column_text(statement,0)]];

在每次迭代中创建一个新的GPS实例

答案 1 :(得分:3)

在每次迭代中创建gps - 对象的新实例:

while(sqlite3_step(statement) == SQLITE_ROW) 
{
     gps = [[GPS alloc] init];

     [gps setJobNo:[NSString stringWithUTF8String:(char *)sqlite3_column_text(statement,0)]];
     [gps setSourceMonitor:[NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 1)]];
     [gps setPositionNo:[NSNumber numberWithInt:sqlite3_column_int(statement,2)]];
     [gps setLatitude:sqlite3_column_double(statement, 3)];
     [gps setLongitude:sqlite3_column_double(statement, 4)];
     NSLog(@"Source Monitor: %@", [gps sourceMonitor]);
     [allGPS addObject:gps];
 }