我有以下 SQL 语句:
const char* sqlStatement = "SELECT ((? - deal.latitude) * (? - deal.latitude) + (? - deal.longitude) * (? - deal.longitude)) AS distance, id, title, shop, latitude, longitude FROM deal WHERE (type = ?) AND category IN (?) AND tribe IN (?) ORDER BY distance LIMIT 20;";
// ...
sqlite3_bind_double(preparedStatement, 1, location.latitude);
sqlite3_bind_double(preparedStatement, 2, location.latitude);
sqlite3_bind_double(preparedStatement, 3, location.longitude);
sqlite3_bind_double(preparedStatement, 4, location.longitude);
sqlite3_bind_int(preparedStatement, 5, type);
sqlite3_bind_text(preparedStatement, 6, [categories UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(preparedStatement, 7, [tribes UTF8String], -1, SQLITE_TRANSIENT);
在这里,第六个和第七个参数导致查询失败,我的意思是我里面的块
while (sqlite3_step(preparedStatement) == SQLITE_ROW) {
不被执行。类别和部落的构建如下:
NSArray* userCategories = [CategoryDataController getUserCategories];
NSMutableString* categories = [[NSMutableString alloc] init];
for (NSNumber *category in userCategories) {
[categories appendString:[[NSString alloc] initWithFormat:@"%@, ", category]];
}
if ([categories length] > 0) {
categories = (NSMutableString *)[categories substringToIndex:[categories length] - 2];
}
NSArray* userTribes = [TribeDataController getUserTribes];
NSMutableString* tribes = [[NSMutableString alloc] init];
for (NSNumber* tribe in userTribes) {
[tribes appendString:[[NSString alloc] initWithFormat:@"%@, ", tribe]];
}
if ([tribes length] > 0)
tribes = (NSMutableString *)[tribes substringToIndex:[tribes length] - 2];
userCategories 和 userTribes 是 NSNumber 的数组,如果我记录部落和类别,我会得到格式良好的字符串,类似于:
1, 2, 3, 4, 5
奇怪的是,我使用 sqlite3_bind_ 函数来构建查询,如下所示:
NSString *sqlStatementNSString = [[NSString alloc] initWithFormat:@"SELECT ((%f - deal.latitude) * (%f - deal.latitude) + (%f - deal.longitude) * (%f - deal.longitude)) AS distance, id, title, shop, latitude, longitude FROM deal WHERE type = %d AND category IN (%@) AND tribe IN (%@) ORDER BY distance LIMIT 20;", location.latitude, location.latitude, location.longitude, location.longitude, type, categories, tribes];
const char *sqlStatement = [sqlStatementNSString UTF8String];
有用!我做错了什么?事先谢谢(请原谅我的英语)。