Iphone 使用sqlite3\u column\u double检索浮点值
我对iPhone编程比较陌生,所以如果这是一个简单的问题,我很抱歉。我试图从SQLite数据库检索表FLOAT类型中的latitude/location列 我可以检索基于字符/文本的db列,但不知道如何检索浮点值。我应该使用NSString或其他东西格式化属性吗?我希望使用CLLocationDistance更下游的浮点值Iphone 使用sqlite3\u column\u double检索浮点值,iphone,objective-c,sqlite,Iphone,Objective C,Sqlite,我对iPhone编程比较陌生,所以如果这是一个简单的问题,我很抱歉。我试图从SQLite数据库检索表FLOAT类型中的latitude/location列 我可以检索基于字符/文本的db列,但不知道如何检索浮点值。我应该使用NSString或其他东西格式化属性吗?我希望使用CLLocationDistance更下游的浮点值 // the array of locations that we will create NSMutableArray *locations = [[[NSMutableA
// the array of locations that we will create
NSMutableArray *locations = [[[NSMutableArray alloc] init] autorelease];
const char *sql = "SELECT locations.name, \
locations.address, \
locations.city, \
locations.state, \
locations.phone, \
locations.zip, \
locations.latitude, \
locations.longitude \
FROM locations \
WHERE category = ?1";
// the sqlite statement object that will hold our result set
sqlite3_stmt *statement;
// prepare the statement to compile the SQL query into byte-code
int sqlResult = sqlite3_prepare_v2(database, sql, -1, &statement, NULL);
// reset query before execution
sqlite3_reset(statement);
// set bind variable
sqlite3_bind_text(statement, 1, [categoryParameter UTF8String], -1, SQLITE_TRANSIENT);
if ( sqlResult== SQLITE_OK) {
// step through the results - once for each row
while (sqlite3_step(statement) == SQLITE_ROW) {
// allocate a location object to add to the pharmacies array
Location *location = [[Location alloc] init];
// the second parameter is the column index (0 based) in
// the result set
char *name = (char *)sqlite3_column_text(statement, 0);
char *address = (char *)sqlite3_column_text(statement, 1);
char *city = (char *)sqlite3_column_text(statement, 2);
char *state = (char *)sqlite3_column_text(statement, 3);
char *phone = (char *)sqlite3_column_text(statement, 4);
char *zipcode = (char *)sqlite3_column_text(statement, 5);
float latitude = (float) sqlite3_column_double(statement, 6);
float longitude = (float) sqlite3_column_double(statement, 7);
// set all attributes of the location
location.name = (name) ? [NSString stringWithUTF8String:name] : @"";
location.address = (address) ? [NSString stringWithUTF8String:address] : @"";
location.city = (city) ? [NSString stringWithUTF8String:city] : @"";
location.state = (state) ? [NSString stringWithUTF8String:state] : @"";
location.phone = (phone) ? [NSString stringWithUTF8String:phone] : @"";
location.zipcode = (zipcode) ? [NSString stringWithUTF8String:zipcode] : @"";
[locations addObject:location];
[location release];
}
// finalize the statement to release its resources
sqlite3_finalize(statement);
纬度和经度是否以度的形式存储在表中?您的Location对象是否有一个名为坐标的CLLocationCoordinate2D属性来放置坐标 如果是,只需直接设置值,因为CLLocationDegrees实际上只是一个双精度值。例如:
CLLocationCoordinate2D coords;
coords.latitude = sqlite3_column_double(statement, 6);
coords.longitude = sqlite3_column_double(statement, 7);
location.coordinates = coords;
纬度和经度是否以度的形式存储在表中?您的Location对象是否有一个名为坐标的CLLocationCoordinate2D属性来放置坐标 如果是,只需直接设置值,因为CLLocationDegrees实际上只是一个双精度值。例如:
CLLocationCoordinate2D coords;
coords.latitude = sqlite3_column_double(statement, 6);
coords.longitude = sqlite3_column_double(statement, 7);
location.coordinates = coords;
我正在存储坐标,例如40.759011和73.9844722Right,这与CLLocationDegrees相同。在Location对象中是否有存储坐标的字段?抱歉,有点混乱。在我的位置object location.h和.m中,我定义了浮动纬度;和浮动经度;好的,那么只需执行location.latitude=sqlite3。。。和location.longitude=sqlite3。。。。但是,我建议您在Location对象中将它们从float更改为CLLocationDegrees,以避免精度损失。奇怪。我以为我以前试过。看起来它正在工作。谢谢你的建议。Float四舍五入并将样本位置放置在距离其应该位置至少0.5英里的位置。我正在存储坐标,例如40.759011和73.9844722右侧,这与CLLocationDegrees相同。在Location对象中是否有存储坐标的字段?抱歉,有点混乱。在我的位置object location.h和.m中,我定义了浮动纬度;和浮动经度;好的,那么只需执行location.latitude=sqlite3。。。和location.longitude=sqlite3。。。。但是,我建议您在Location对象中将它们从float更改为CLLocationDegrees,以避免精度损失。奇怪。我以为我以前试过。看起来它正在工作。谢谢你的建议。浮子将其四舍五入,并将样本放置在距离其应放置位置至少0.5英里的位置。