我想通过字符串匹配从sqlite3数据库选择记录。但是如果我在where子句中使用'=',我发现sqlite3是区分大小写的。有人能告诉我如何使用字符串比较不区分大小写吗?


这不是特定于sqlite,但你可以这样做

SELECT * FROM ... WHERE UPPER(name) = UPPER('someone')

你可以这样做:

SELECT * FROM ... WHERE name LIKE 'someone'

(这不是解决方案,但在某些情况下非常方便)

"The LIKE operator does a pattern matching comparison. The operand to the right contains the pattern, the left hand operand contains the string to match against the pattern. A percent symbol ("%") in the pattern matches any sequence of zero or more characters in the string. An underscore ("_") in the pattern matches any single character in the string. Any other character matches itself or its lower/upper case equivalent (i.e. case-insensitive matching). (A bug: SQLite only understands upper/lower case for ASCII characters. The LIKE operator is case sensitive for unicode characters that are beyond the ASCII range. For example, the expression 'a' LIKE 'A' is TRUE but 'æ' LIKE 'Æ' is FALSE.)."

SELECT * FROM ... WHERE name = 'someone' COLLATE NOCASE

你可以在SELECT查询中使用COLLATE NOCASE:

SELECT * FROM ... WHERE name = 'someone' COLLATE NOCASE

此外,在SQLite中,您可以在创建表时通过在列定义中指定collate nocase来指示列不区分大小写(其他选项是二进制(默认值)和rtrim;见这里)。您也可以在创建索引时指定collate nocase。例如:

create table Test
(
  Text_Value  text collate nocase
);

insert into Test values ('A');
insert into Test values ('b');
insert into Test values ('C');

create index Test_Text_Value_Index
  on Test (Text_Value collate nocase);

涉及Test的表达式。Text_Value现在应该不区分大小写。例如:

sqlite> select Text_Value from Test where Text_Value = 'B';
Text_Value      
----------------
b               

sqlite> select Text_Value from Test order by Text_Value;
Text_Value      
----------------
A               
b               
C    

sqlite> select Text_Value from Test order by Text_Value desc;
Text_Value      
----------------
C               
b               
A               

优化器还可能利用索引对列进行不区分大小写的搜索和匹配。你可以使用explain SQL命令来检查,例如:

sqlite> explain select Text_Value from Test where Text_Value = 'b';
addr              opcode          p1          p2          p3                               
----------------  --------------  ----------  ----------  ---------------------------------
0                 Goto            0           16                                           
1                 Integer         0           0                                            
2                 OpenRead        1           3           keyinfo(1,NOCASE)                
3                 SetNumColumns   1           2                                            
4                 String8         0           0           b                                
5                 IsNull          -1          14                                           
6                 MakeRecord      1           0           a                                
7                 MemStore        0           0                                            
8                 MoveGe          1           14                                           
9                 MemLoad         0           0                                            
10                IdxGE           1           14          +                                
11                Column          1           0                                            
12                Callback        1           0                                            
13                Next            1           9                                            
14                Close           1           0                                            
15                Halt            0           0                                            
16                Transaction     0           0                                            
17                VerifyCookie    0           4                                            
18                Goto            0           1                                            
19                Noop            0           0                                            

如果列的类型是char,那么你需要用空格来附加你正在查询的值,请参考这里的问题。除此之外,还可以使用COLLATE NOCASE或其他解决方案(upper()等)。

另一种选择是创建自己的自定义排序规则。然后可以在列上设置排序规则,或将其添加到选择子句中。它将用于排序和比较。

这可以用来使'VOILA'像'voilà'。

http://www.sqlite.org/capi3ref.html#sqlite3_create_collation

排序函数必须返回一个整数,如果第一个字符串分别小于、等于或大于第二个字符串,则该整数为负、零或正。

另一种选择可能对您的情况有意义,也可能没有意义,那就是实际上有一个单独的列,其中包含现有列的预先低分值。可以使用SQLite函数LOWER()填充该列,然后可以对该列执行匹配。

显然,它增加了冗余和潜在的不一致性,但如果您的数据是静态的,它可能是一个合适的选择。

可以使用like查询将各自的字符串与表值进行比较。

Select column name from table_name where column name like '各自的比较值';

这对我很有效。 从表_name中选择测试名

简单地说,你可以在SELECT查询中使用COLLATE NOCASE:

SELECT * FROM ... WHERE name = 'someone' COLLATE NOCASE

像这样使用

 "select * from $pwsXDataHistory where type = '$type' COLLATE NOCASE and $t_uStatus != '$DELETE' order by $t_name COLLATE NOCASE asc ");