получение значений с плавающей запятой с использованием sqlite3_column_double - PullRequest
1 голос
/ 25 марта 2011

Я относительно новичок в программировании iPhone, поэтому прошу прощения, если это кажется простым вопросом. В моей таблице есть столбцы широты и местоположения (тип FLOAT), которые я пытаюсь получить из базы данных SQLite.

Я могу получить свои столбцы БД на основе символов / текста, но не знаю, как получить значения с плавающей точкой. Должен ли я форматировать атрибуты, используя 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);

1 Ответ

5 голосов
/ 26 марта 2011

Широта и долгота хранятся в таблице в градусах? Есть ли в вашем объекте Location свойство CLLocationCoordinate2D, которое называется, скажем, координаты для помещения координат?

Если да, просто установите значения напрямую, потому что CLLocationDegrees на самом деле просто удваивается. Например:

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

location.coordinates = coords;
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...