微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

我试图将我的sqlite3表重现为保存到手机文档中的.csv文件

为了做到这一点,我试图在文件的末尾附加一行,每次在我的数组中找到一个对象,这是有效的 – 但是似乎再一次重现最后一行的确切数量我的sql表中的行。

这是sqlite选择查询

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

解决方法

您在重复循环中总是使用相同的GPS实例,因为引用语义会覆盖对象中的数据,并且在每次迭代中将相同的对象添加到数组中。

移动线

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

就在线之前

[gps setJobNo:[Nsstring stringWithUTF8String:(char *)sqlite3_column_text(statement,0)]];

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐