我正在研究Core Location框架,尝试计算坐标之间的距离并更新到数据库的距离(sqlite3.0)。但由于某种原因,我得到了冗余数据的更新。我不确定代码是否正确更新。我需要一些认真帮助的人。谢谢。
这是我的代码:
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];
}
答案 0 :(得分:0)