2011-03-25 3 views
1

저는 iPhone 프로그래밍이 처음인데, 간단한 질문 인 것처럼 사과합니다. 내 위도/위치 열을 내 SQLite 데이터베이스에서 검색하려면 노력하고 내 테이블 (FLOAT 형식) 있습니다.sqlite3_column_double을 사용하여 float 값 검색

문자/텍스트 기반 db 열을 검색 할 수 있지만 플로트 값을 검색하는 방법을 알 수 없습니다. NSString 또는 다른 것을 사용하여 특성을 포맷해야합니까? CLLocationDistance를 사용하여 float 값을 추가로 다운 스트림으로 사용하고 싶습니다.

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

위도와 경도가 테이블에 각도로 저장되어 있습니까? 위치 개체에 좌표라고 불리는 CLLocationCoordinate2D 속성이 있습니까?

그렇다면 CLLocationDegrees가 실제로는 double이기 때문에 직접 값을 설정하십시오. 예 :

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

+0

바로 CLLocationDegrees와 같습니다. 위치 객체에 좌표를 저장할 필드가 있습니까? – Anna

+0

죄송합니다. 조금 혼란 스럽습니다. 내 위치 객체 (location.h 및 .m)에서 float latitude를 정의했습니다. 그리고 경도를 떠내려 라. – Jeremy