我正在使用 python/django 处理 sqlite 数据库。我需要得到我的结果,其中包含要正确排序的德语变音符号 (ä,ö,ü)(而不是末尾的变音符号)。在网上阅读不同的文章,我什至不确定这是否可能。
因此,对此的任何建议/说明表示赞赏。我已经研究过 create_collation
等文档,但找不到任何对“初学者”有用的示例。此外,如果可能的话,我想知道如何对现有表格应用必要的修改!
最佳答案
So any advise/instructions on that are appreciated. I already studied the docs for create_collation etc. but I couldn't find any helpful examples for "beginners".
要使用 sqlite3
创建排序规则,您需要一个类似于 C 的 strcmp
的函数.
def stricmp(str1, str2):
str1 = str1.lower()
str2 = str2.lower()
if str1 == str2:
return 0
elif str1 < str2:
return -1
else:
return 1
db = sqlite3.connect(':memory:')
# SQLite's default NOCASE collation is ASCII-only
# Override it with a (mostly) Unicode-aware version
db.create_collation('NOCASE', stricmp)
请注意,尽管此排序规则可以正确处理 'ü' == 'Ü'
,但它仍然会有 'ü' > 'v'
,因为字母仍然大小写折叠后按 Unicode 代码点顺序排序。编写德语友好的整理功能留给读者作为练习。或者更好的是,给现有 Unicode 库的作者。
Furthermore, if it is possible I'd like to know how to apply the necessary modifications on already existing tables!
如果您的索引使用了您已覆盖的排序规则,则只需修改数据库。 删除
该索引并重新创建
它。
请注意,任何具有UNIQUE
(或PRIMARY KEY
)约束的列都将具有隐式索引。
关于python - 使用变音符号在 SQLite3 中排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3763838/