我正在从 sqlite 数据库中检索成分并将它们插入到我的表视图中。每次用户编辑UISearchBar
位于屏幕上的搜索栏 ( ) 时都会执行此操作。
查询方法:
-(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;
}
这就是我格式化查询和更新表格视图的方式:
-(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];
}