【发布时间】:2023-03-23 12:26:02
【问题描述】:
我有以下方法在 iOS6 和 iOS7 上完美运行
#import "MapaViewController.h"
#import "sqlite3.h"
#import "CelulaAlertas.h"
#import "OBRadar.h"
#import "OBDownloadBase.h"
#define DEG2RAD(degrees) (degrees * 0.01745327) // degrees * pi over 180
static void distanceFunc(sqlite3_context *context, int argc, sqlite3_value **argv)
{
// check that we have four arguments (lat1, lon1, lat2, lon2)
assert(argc == 4);
// check that all four arguments are non-null
if (sqlite3_value_type(argv[0]) == SQLITE_NULL || sqlite3_value_type(argv[1]) == SQLITE_NULL || sqlite3_value_type(argv[2]) == SQLITE_NULL || sqlite3_value_type(argv[3]) == SQLITE_NULL) {
sqlite3_result_null(context);
return;
}
// get the four argument values
double lat1 = sqlite3_value_double(argv[0]);
double lon1 = sqlite3_value_double(argv[1]);
double lat2 = sqlite3_value_double(argv[2]);
double lon2 = sqlite3_value_double(argv[3]);
// convert lat1 and lat2 into radians now, to avoid doing it twice below
double lat1rad = DEG2RAD(lat1);
double lat2rad = DEG2RAD(lat2);
// apply the spherical law of cosines to our latitudes and longitudes, and set the result appropriately
// 6378.1 is the approximate radius of the earth in kilometres
sqlite3_result_double(context, acos(sin(lat1rad) * sin(lat2rad) + cos(lat1rad) * cos(lat2rad) * cos(DEG2RAD(lon2) - DEG2RAD(lon1))) * 6378.1);
}
在我调用的初始方法中:
sqlite3_create_function(db, "distance", 4, SQLITE_UTF8, NULL, &distanceFunc, NULL, NULL);
在我的选择上:
NSString *qsql= [NSString stringWithFormat:@"SELECT * FROM Radares WHERE distance(Latitude, Longitude, %@, %@)<1 ORDER BY distance(Latitude, Longitude, %@, %@)",lat,longi,lat,longi];
NSLog(@"%@",qsql);
sqlite3_stmt *statement;
if (sqlite3_prepare_v2( db, [qsql UTF8String], -1,
&statement, nil) == SQLITE_OK)
{
while (sqlite3_step(statement) == SQLITE_ROW)
{.....
只有在 iOS8 上没有调用“distanceFunc”方法。有人可以帮我理解为什么吗?
我在 sqlite 中得到两个不同的错误代码,准备在 iOS 7 和 iOS 8 中运行相同的代码:
ios7
2014-09-19 09:18:08.841 Mapa Radar[511:60b] 数据库返回错误 0:不是错误 2014-09-19 09:18:08.841 Mapa Radar[511:60b] 数据库返回错误 0:不是错误 2014-09-19 09:18:08.974 Mapa Radar[511:60b] Accucary 5 2014-09-19 09:18:08.975 Mapa Radar[511:60b] SELECT * FROM Radares WHERE distance2(纬度,经度,-23.111100,-46.849126)
ios8
2014-09-19 09:14:51.631 Mapa Radar[609:76761] 数据库返回错误 0:不是错误 2014-09-19 09:14:51.633 Mapa Radar[609:76761] 数据库返回错误 0:不是错误 2014-09-19 09:14:52.252 Mapa 雷达[609:76761] Accucary 65 2014-09-19 09:14:52.258 Mapa Radar[609:76761] SELECT * FROM Radares WHERE distance2(纬度,经度,-23.516678,-47.477784)
为什么在 iOS 8 上,我的表 Radares 没有被识别,但在 iOS 6 和 7 上它可以工作?
【问题讨论】:
-
1) 检查
sqlite3_create_function的返回值,确保函数已创建。 2) 运行查询时是否到达while循环的内部? -
该函数没有被创建,但仅在 iOS8 上。“static void distanceFunc”中的代码在 iOS8 上不运行。在调试模式下我检查了它..
-
sqlite3_create_function的返回值是多少? -
rmaddy 的意思是 sqlite3_create_function 有返回码!! 返回什么值?
int sqlite3_create_function(...--int是返回码。您应该始终检查 SQLite 返回码。 -
另外,你从 prepare 和 step 得到什么返回码?