SQLite德语特殊字符

时间:2014-06-17 11:27:58

标签: vb.net sqlite collation system.data.sqlite

一些德语单词包含“ß”字符。

然而,有时人们会写“ss”而反之亦然。 虽然不完全合法,但这是一种常见做法。

例如,有些人例如写“außer”,有些人写“ausser”。

我想在我的SQLite DB中很好地处理这个问题。

因此我创建了一个自定义LATIN_NOCASE排序规则,但它没有按预期工作:

我插入了一个名为“außer”的单词,我希望能够在搜索“s”或“ss”时找到它。

但是,我的选择不会返回任何结果。

有人看到我哪里出错吗? 比较如下,还是我需要做一些特别的事情?

谢谢。

Imports System.Data.SQLite
Imports System.Globalization

<SQLiteFunction(FuncType:=FunctionType.Collation, Name:="LATIN_NOCASE")> _
Public Class SQLiteCaseInsensitiveCollation
    Inherits SQLiteFunction

    Private Shared ReadOnly _cultureInfo As CultureInfo = CultureInfo.CreateSpecificCulture("de-DE")

    Public Overrides Function Compare(x As String, y As String) As Integer
        Return String.Compare(x, y, _cultureInfo, CompareOptions.IgnoreCase)
    End Function

End Class

Public Class Form1

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load

        SQLiteFunction.RegisterFunction(GetType(SQLiteCaseInsensitiveCollation))

        Dim c As New SQLite.SQLiteConnection("Data source=:memory:")
        c.Open()

        Using nCmd As SQLite.SQLiteCommand = c.CreateCommand
            With nCmd
                .CommandText = "CREATE TABLE foo (word TEXT COLLATE LATIN_NOCASE)"
                .ExecuteNonQuery()
            End With
        End Using

        Using nCmd As SQLite.SQLiteCommand = c.CreateCommand
            With nCmd
                .CommandText = "CREATE INDEX 'idx_foo_word' ON 'foo' (word COLLATE LATIN_NOCASE)"
                .ExecuteNonQuery()
            End With
        End Using

        Using nCmd As SQLite.SQLiteCommand = c.CreateCommand
            With nCmd
                .CommandText = "INSERT INTO foo (word) VALUES ('außer')"
                .ExecuteNonQuery()
            End With
        End Using

        Using nCmdSel As SQLite.SQLiteCommand = c.CreateCommand
            With nCmdSel
                .CommandText = "SELECT * FROM foo WHERE word LIKE '%ss%' COLLATE LATIN_NOCASE"

                Using r As SQLite.SQLiteDataReader = .ExecuteReader
                    Do While r.Read
                        MsgBox(r("word"))
                    Loop
                End Using
            End With
        End Using

    End Sub
End Class

1 个答案:

答案 0 :(得分:0)

文化de-DE不会忽视ss和ß之间的差异。

在进行比较之前,您必须自己修改字符串。