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


当前回答

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

其他回答

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

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

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

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

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

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

你可以这样做:

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