gpt4 book ai didi

iphone - 使用sqlite3_column_double检索浮点值

转载 作者:行者123 更新时间:2023-12-03 06:49:27 24 4
gpt4 key购买 nike

我对iPhone编程比较陌生,因此如果这看起来很简单,我深表歉意。我的表格(FLOAT类型)中有纬度/位置列,我试图从我的SQLite数据库中检索这些列。

我可以检索基于字符/文本的数据库列,但不知道如何检索浮点值。我应该使用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);

最佳答案

纬度和经度是否存储为表格中的度数?您的Location对象是否具有CLLocationCoordinate2D属性(例如,称为可放置坐标的坐标)?

如果是,则直接设置值,因为CLLocationDegrees实际上只是一个 double 值。例如:

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

location.coordinates = coords;

关于iphone - 使用sqlite3_column_double检索浮点值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5437876/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com