.net 中的 SQLite 不区分大小写的 order by
我正在使用 SQLite.net ( http://sqlite.phxsoftware.com ) 的 C# 程序中使用 SQLite 。
默认情况下,SQLite select order by 子句排序是区分大小写的,我希望结果排序不区分大小写,我找到了“COLLATE NOCASE”,但文档说它只会处理 ascii 范围内的英文字符,我想要真正的语言国际不区分大小写排序使用 CultureInfo.CurrentCulture 排序规则(使用 String.Compare 即可解决问题)。
I'm using SQLite from a C# program using SQLite.net ( http://sqlite.phxsoftware.com ).
By default SQLite select order by clause sort is case sensitive, I want the result to sort case insensitive, I found "COLLATE NOCASE" but the documentation says it will only handle English characters in the ascii range, I want true linguistic international case insensitive sort using the CultureInfo.CurrentCulture collation (making it use String.Compare will do the trick).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我相信当前版本的 SQLite 中没有提供这种排序规则。 因此,最明智的计划似乎是从查询中删除排序,然后在纯 .Net 中进行排序,您可以完全控制和访问线程的区域性信息等构造。
由于两者都发生在同一进程中,因此除非您的数据集非常大,否则不会对性能产生太大影响。
SQLite 3 确实允许用户定义的排序函数,并且这些函数可以在 SQLite.Net 中作为 .net 函数完成,但跨托管/非托管边界来回调用的开销相当大。 这是一个人们尝试用 C++ 实现的操作。 除非您可以访问其他人在 C++ 中经过充分测试且稳定的 unicode 文化敏感排序,否则我建议尽可能坚持使用简单的排序方法。
当然,如果用户定义的排序规则的性能足以满足您当前的需求,那么就使用它。
如果您想变得更奇特,请发布脚本:
有一个受祝福的 SQLite 版本,集成了 ICU 库 对于 ordering/like/upper/lower 的“适当”unicode 支持,但您需要将其集成到用作 .Net 包装器支持的 sqlite 代码中。 这很多并不容易。
I believe such collation is not provided in current versions of SQLite. As such it would seem that the most sensible plan is to remove the sort from the query and instead sort afterwards in pure .Net where you have full control and access to constructs like the thread's culture info.
Since both are happening in the same process this shouldn't make a big difference in performance terms unless your dataset is very large.
SQLite 3 does allow user defined collation functions, and these can be done in SQLite.Net as .net functions but the overhead of calling back and forth across the managed/unmanaged boundary is considerable. Here is one persons attempts to do it in c++. Unless you have access to someone else's well tested and stable unicode culture sensitive sort in C++ I suggest sticking to the simple sort after approach where possible.
Of course if the performance of the user defined collation is more than enough for your present needs then go with that.
Post script if you fancy getting fancy:
There is a blessed build of SQLite which integrates the ICU library for 'proper' unicode support on ordering/like/upper/lower but you would need to integrate that into the sqlite code used as the backing for the .Net wrapper. This many not be easy.
SQLite.NET 允许您在 C# 中定义可在 SQL 查询中使用的自定义函数。 因此,应该可以在 C# 中编写一个 custom_upper 或 custom_lower 函数来处理当前区域性排序规则的大小写转换...
SQLite.NET allows you to define custom functions in C# that you can use in SQL queries. So it should be possible to write a custom_upper or custom_lower function in C# that would handle the case conversion with the current culture collation...
您可以简单地使用
lower()
sqlite 函数:You can simply use the
lower()
sqlite function:是的,这比您想象的要复杂得多。 在某些文化中,规则是不同的,如果不知道你想要将其建立在哪种文化的基础上,那就不可能了。 考虑一下著名的(国际人)土耳其语的例子。
Aye, this is much more complex that you would guess it is. In some cultures the rules are different and without know what culture you want to base it off of it becomes impossible. Consider the famous (amoung i18n people) example of Turkish.
您可以使用 ToLower() 方法来快速获得结果:
You can use ToLower() method for getting quick result: