我正在从sqlite数据库中检索成分并将它们插入到我的表视图中。每次用户编辑位于屏幕上的搜索栏(UISearchBar
)时都会执行此操作。
QueryMethod :
-(NSArray*)sqliteInfo:(NSString*)predicateString{
NSMutableArray *retval = [[[NSMutableArray alloc] init] autorelease];
sqlite3_stmt *statement;
if (sqlite3_prepare_v2(_database, [predicateString UTF8String], -1, &statement, nil)
== SQLITE_OK) {
while (sqlite3_step(statement) == SQLITE_ROW) {
char *nameChars = (char *) sqlite3_column_text(statement, 0);
NSString *name = [[NSString alloc] initWithUTF8String:nameChars];
sqlite *info = [[sqlite alloc]
initWithName:name carbs:nil fat:nil kcal:nil];
[retval addObject:info];
[name release];
}
sqlite3_finalize(statement);
}
return retval;
}
这是我格式化查询并更新tableview的方式:
-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{
if (searchText.length < 2) {
return;
} else {
[self.nutritionList removeAllObjects];
NSString *formatString = [[@"'%"stringByAppendingString:searchText]stringByAppendingString:@"%' ORDER BY namn COLLATE NOCASE"];
NSArray *array = [[NutritionsDB database] sqliteInfo:[NSString stringWithFormat:@"Select namn from livsmedel where namn like %@", formatString ]];
for (sqlite *info in array) {
[self.nutritionList addObject:info.name];
}
[self.tableView reloadData];
}
}
正如您在formatString
中看到的那样,我正在尝试对结果进行排序并忽略字符串的情况
ORDER BY namn COLLATE NOCASE
这很好用,但是瑞典字母Å,Ä,Ö,忽略的情况失败了。
如果我搜索'Ål',我得到的结果与搜索får'ål'时的效果不一样。 换句话说,当我搜索'ål'时,成分'Ål'不会显示
我的问题: 如何让SQLite忽略大小写,包括外国(瑞典)字母?
希望以上内容有道理,
感谢。
修改
采用以下方法:
-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{
if (searchText.length < 2) {
return;
} else {
NSString *firstChar = [searchText substringWithRange:NSMakeRange(0, 1)];
if([firstChar isEqualToString:@"å"] ||
[firstChar isEqualToString:@"Å"] ||
[firstChar isEqualToString:@"ä"] ||
[firstChar isEqualToString:@"Ä"] ||
[firstChar isEqualToString:@"ö"] ||
[firstChar isEqualToString:@"Ö"]){
NSString *lowerString = [firstChar lowercaseString];
NSString *upperString = [firstChar uppercaseString];
NSString *newSearchText = [searchText stringByReplacingCharactersInRange:NSMakeRange(0, 1) withString:lowerString];
NSString *newSearchText_upper = [searchText stringByReplacingCharactersInRange:NSMakeRange(0, 1) withString:upperString];
NSString *formatString = [[[[[@"'%"stringByAppendingString:newSearchText]stringByAppendingString:@"%'"]
stringByAppendingString:@" OR namn like'%"]stringByAppendingString:newSearchText_upper]stringByAppendingString:@"%' ORDER BY namn COLLATE NOCASE"];
NSLog(formatString);
[self.nutritionList removeAllObjects];
NSArray *array = [[NutritionsDB database] sqliteInfo:[NSString stringWithFormat:@"Select namn from livsmedel where namn like %@", formatString ]];
for (sqlite *info in array) {
[self.nutritionList addObject:info.name];
}
[self.tableView reloadData];
return;
}
}
[self.nutritionList removeAllObjects];
NSString *formatString = [[@"'%"stringByAppendingString:searchText]stringByAppendingString:@"%' ORDER BY namn COLLATE NOCASE"];
NSArray *array = [[NutritionsDB database] sqliteInfo:[NSString stringWithFormat:@"Select namn from livsmedel where namn like %@", formatString ]];
for (sqlite *info in array) {
[self.nutritionList addObject:info.name];
}
[self.tableView reloadData];
}
答案 0 :(得分:1)
NOCASE - 与二进制相同,除了在执行比较之前将ASCII的26个大写字符折叠为它们的小写等价物。请注意,只有ASCII字符是大小写折叠的。 由于所需表格的大小,SQLite不会尝试执行完整的UTF大小写折叠。
您最好的选择可能是compile your own SQLite与ICU extensions。
答案 1 :(得分:0)
在查询方法中,而不是[predicateString UTF8String]
尝试使用
[predicateString cStringUsingEncoding:NSISOLatin1StringEncoding]
或
[predicateString cStringUsingEncoding:NSUTF8StringEncoding]