使用 SQLite 在 Qt 中进行不区分大小写的搜索
我有一个 SQLite 数据库,其中包含西里尔字母行。 例如:
CREATE TABLE customer (
id INTEGER PRIMARY KEY,
name TEXT,
surname TEXT
);
假设其中两行是这样的:
"153 | Иван | Попов"
"243 | Ivan | Popov"
实际上是俄语和英语写的同一个名字和姓氏。
我在我的程序中使用 C++ 和 Qt。我想选择名称或姓氏列中包含特定字符串的行。
由于我使用QSqlTableModel
,我只是将过滤器(它只是 SQL 查询的 WHERE
部分)与 SQL 查询结合使用:
void filterTable(QString filter)
{
QString query;
if(!filter.isEmpty())
{
//I've added the wildcards to be able to search
//using only subset of the name or surname
query += "name LIKE '%" + filter "%' OR ";
query += "surname LIKE '%" + filter "%'";
}
mySqlTableModel->setFilter(query);
}
当我将过滤器设置为“ivan”时,它会显示正确的行(ID 243)。但是,当我尝试对“иван”执行相同操作时,它不会显示,而“Иван”、“ван”等则显示正确的条目。看起来该查询仅适用于西里尔字母中区分大小写的匹配,而它适用于拉丁字母。
我知道 ICU 并且已经启用它。它有效:
$ sqlite3
$ SELECT 'Иван' like 'иван';
1
所以我仍然无法理解为什么当我使用西里尔字母时我的程序中的查询区分大小写?它与 Qt 中的 SQL 查询实现有某种关系吗?
I have a SQLite database with cyrillic rows in it.
For example:
CREATE TABLE customer (
id INTEGER PRIMARY KEY,
name TEXT,
surname TEXT
);
Suppose two of the rows are like this:
"153 | Иван | Попов"
"243 | Ivan | Popov"
It is actually the same name and surname written in russian and english.
I use c++ and Qt in my program. I want to select rows that contain particular string in name or surname columns.
Since I use QSqlTableModel
I simply use filter (which is just WHERE
part of SQL query) with SQL query:
void filterTable(QString filter)
{
QString query;
if(!filter.isEmpty())
{
//I've added the wildcards to be able to search
//using only subset of the name or surname
query += "name LIKE '%" + filter "%' OR ";
query += "surname LIKE '%" + filter "%'";
}
mySqlTableModel->setFilter(query);
}
When I set the filter to 'ivan' it shows the correct row (with id 243). However, when I attempt to do the same with 'иван' it does not show, while 'Иван', 'ван' and etc show correct entries. It seems like the query works only with case sensitive matches in cyrillic, while it works perfectly fine with latin letters.
I am aware of ICU and already enabled it. And it works:
$ sqlite3
$ SELECT 'Иван' like 'иван';
1
So I still can not understand why my queries in my program are case sensitive when I use cyrillic letters? Is it somehow related to SQL query implementation in Qt?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
默认情况下,SQLite 仅支持 ASCII 字符不区分大小写。如果您想要支持 Unicode 大小写,则必须定义您自己的
UPPER
/LOWER
函数 和排序规则。By default, SQLite only supports case-insensitivity for ASCII characters. If you want support for Unicode casing, you'll have to define your own
UPPER
/LOWER
functions and collation.