2011-03-25 126 views
1

我對iPhone編程比較陌生,所以我很抱歉如果這看起來是一個簡單的問題。我在我的表(FLOAT類型),我想從我的SQLite數據庫中檢索緯度/位置列。使用sqlite3_column_double檢索浮點值

我可以檢索我的基於字符/文本的數據庫列,但不知道如何檢索我的浮點值。我應該使用NSString或其他格式來設置屬性的格式嗎?我希望使用CLLocationDistance進一步下游的浮點值。

// 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); 

回答

5

緯度和經度是以度表的形式存儲的嗎?你的Location對象是否有一個名爲CLLocationCoordinate2D的屬性,比如座標來放置座標?

如果是,直接設置值,因爲CLLocationDegrees實際上只是一個雙精度值。例如:

CLLocationCoordinate2D coords; 
coords.latitude = sqlite3_column_double(statement, 6); 
coords.longitude = sqlite3_column_double(statement, 7); 

location.coordinates = coords; 
+0

我存儲的座標是40.759011和73.9844722 – Jeremy 2011-03-25 21:28:55

+0

對,就和CLLocationDegrees一樣。你有位置對象中的字段來存儲座標嗎? – Anna 2011-03-25 21:31:18

+0

對不起,有點困惑。在我的位置對象(location.h和.m)中,我定義了float緯度;和經度; – Jeremy 2011-03-25 22:09:11