-->

How to implement the accent/diacritic insensitive

2020-02-06 11:16发布

问题:

Is there a way to do an accent/diacritic insensitive search in sqlite?

Googling, I've found this, but I sincerely don't know how to create my "collation function" in C#. I'm trying create the pt-br collation for Sqlite...

回答1:

If you are using System.Data.Sqlite you can use a custom collation sequence and modify it for your needs ... an example:

/// <summary>
/// User-defined collating sequence using the current UI culture.
/// </summary>
[SQLiteFunction(Name = "MYSEQUENCE", FuncType = FunctionType.Collation)]
class MySequence : SQLiteFunction
{
  public override int Compare(string param1, string param2)
  {
    return String.Compare(param1, param2, true);
  }
}

Your SQL query to use the custom collation sequence above might look like this: SELECT * FROM MyTable ORDER BY MyChineseColumn COLLATE MYSEQUENCE DESC

(source: http://sqlite.phxsoftware.com/forums/p/862/3779.aspx#3779)

You just can set collation at table creation and build indices on that column. But remember, you won't be able to access table from other sqlite engine where collation not defined.



回答2:

While it's possible to write your own collation, that might easily be slower than using a suitable system-supplied one -- doesn't SQL_Latin1_General_CP1_CI_AS serve your needs, for example? If it does, just use suitable COLLATE clauses (be that on your SELECT or CREATE TABLE queries...) and you should be all set.