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实例

相关文章

在有效期内的苹果开发者账号(类型为个人或者公司账号)。还...
Appuploader官网--IOS ipa上传发布工具,证书制作工具跨平台...
苹果在9月13号凌晨(北京时间)发布 iOS 16,该系统的设备可...
计算机图形学--OpenGL递归实现光线追踪
Xcode 14打出来的包在低版本系统运行时会崩溃,报错信息是Li...