0

I am working on Core Location framework, trying to calculate distance between the coordinates and updating the distance onto the database(sqlite3.0). But for some reason i get redundant data getting updated. Im not sure if the code is correct for updating. I need some serious help guys. Thank you.

Here goes my code:

    if(sqlite3_open([writableDBPath UTF8String],&controller)==SQLITE_OK){
    NSString *str=[NSString stringWithFormat:@"select * from coordinate_table"];
    sqlite3_prepare_v2(controller, [str UTF8String], -1, &statement, NULL);
    int i=0;

    while(sqlite3_step(statement)==SQLITE_ROW){
        newlat=sqlite3_column_double(statement, 0);
        newlongi=sqlite3_column_double(statement, 1);

        CLLocation *newLoc=[[CLLocation alloc]initWithLatitude:newlat longitude:newlongi];

        CLLocationDistance dis=[currLoc distanceFromLocation:newLoc];

        NSString *dist=[NSString stringWithFormat:@"%f",dis];





        [marr insertObject:dist atIndex:i];

        NSString *sql=[NSString stringWithFormat:@"update coordinate_table set distance=%f where latitude=%f and longitude=%f",dis,newlat,newlongi];
        NSLog(@"%@",sql);
        int res=sqlite3_exec(controller, [sql UTF8String], NULL, NULL, NULL);

        NSLog(@"%d",res);
        i++;

    }
    NSLog(@"Distance Array %@",marr);
}
sqlite3_finalize(statement);
sqlite3_close(controller);
[tView reloadData];

}
4

1 回答 1

0

我建议使用FMDB Lib 来处理 sqlite。它是强大的 sqlite 包装器。使用起来很简单!看这个问题:

FMDB 如何让 sqlite 更简单 iOS?

希望对你有帮助)

于 2013-05-15T06:45:00.877 回答