SQLite 3 : Character Issue While Ordering By Records

标签 sqlite collation

在我的 SQLite 3 数据库中,我有一些带有土耳其语字符的记录,例如“Ö”、“Ü”、“İ”等。当我使用 SELECT * FROM TABLE ORDER BY COLUMN_NAME 选择我的值时查询,以这些字符开头的记录在最后。

通常,他们应该在每个字母的无点版本之后出现。就像“Ö”在“O”之后一样,“Ü”在“U”之后。

与区域设置有关吗?有没有办法控制这些设置?

我在 Firefox 中使用 SQLite Manager 来管理我的数据库。

提前致谢。

附言我知道这不是 SQLite 的解决方案,但对于那些需要在 Objective-C 中使用 SQLite DB 的人来说,他们可以在从 SQLite DB 获取后对数据数组进行排序。这是一个很好的解决方案:How to sort an NSMutableArray with custom objects in it?

最佳答案

不幸的是,似乎没有直接的解决方案。至少对于 iOS。但是有方法可以遵循。

在我订阅了 SQLite 的邮件列表后,名为 Jean-Christophe Deschamps 的用户收到了以下回复:

"In my SQLite 3 Database, I have some records with Turkish characters such as "Ö", "Ü", "İ" etc. When I select my values with 'SELECT * FROM TABLE ORDER BY COLUMN_NAME' query, the records that begin with these characters are coming at the end."

Bare bone SQLite only collates correctly on the lower ASCII charset. While that's fine for plain english, it doesn't work for most of us.

"Normally, they should've come after the letter that is dot-less version of each. Like "Ö" is after "O", "Ü" is after "U". Is it something about regional settings? Is there a way to control these settings?"

You have the choice among some ways to get it right or close to right for your language(s):

o) use ICU either as an extension (for third-party managers) or linked to your application. Advantages: it works 100% correctly for a given language at a time in each operation. Drawbacks: it's huge and slow and it requires you register a collation for every language you deal with. Also it won't work well for columns containing several non-english languages.

o) write your own collation(s) invoking your OS' ICU routines to collate strings. Advantages: doesn't bloat your code with huge libraries. Drawbacks: requires you write this extension (in C or something), same other drawbacks as ICU.

o) If you use Windows, download and use the functions in the extension I wrote for a close-to-correct result. Advantages: it's small, fairly fast and ready to use, it is language- independant yet works decently well for many languages at the same time; it also offers a number of Unicode-aware string manipulation functions (unaccenting or not) functions, a fuzzy search function and much more. Comes as a C source and x86 DLL, free for any purpose. Drawback: it probably doesn't work 100% correctly for any language using more than "vanilla english letters": your dotless i will collate along dotted i, for instance. It's a good compromise between absolute correctness for ONE language and "fair" correctness for most languages (including some asian languages using diacritics) Download: http://dl.dropbox.com/u/26433628/unifuzz.zip

"I use SQLite Manager in Firefox to manage my DB."

My little extension will work with this one. You might also want to try SQLite Expert which has ICU built-in (at least in its Pro version) and much more.

关于SQLite 3 : Character Issue While Ordering By Records,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7813069/

相关文章:

Android GreenDAO 生成额外的 ID 属性

php - SQLSTATE[HY000] : General error: 1 table posts has no column named *

android - 无法将现有 SQLite 数据库导入 Android 应用程序

Mysql转换表,排序规则不变

mysql - 在 MySQL 中如何为 WHERE-IN 子句指定排序规则?

android - Xamarin android Sqlite.net C# 如何将同步查询转换为异步任务

iphone - 如何使用iPhone中的where子句从sqlite表中获取数据

MySQL "illegal mix of collations"使用源文件中的变量

mysql - 相同的查询,但 ORDER BY 产生不同的结果

java - 使用排序规则对马其顿字母表进行排序