How to solve the case-sensitivity problem when comparing SQLite strings. sqlite strings are case-insensitive.
Most databases are case insensitive when comparing strings. However, when we recently used SQLite, we found that the opposite is true.
Assume that the structure and value of the table User are as follows:
Execute the following SQL statement:
Copy codeThe Code is as follows:
SELECT * FROM [User] WHERE UserName = 'user1'
The result is that no record is found. Obviously, SQLite is case sensitive by default when comparing strings. This will have a negative impact on the application. For example, the user must strictly follow the case when entering the USER name, which is a bad user experience. The USER table may have both users, which is easy to cause confusion.
Since SQLite is a database that has emerged in recent years, there is very little Chinese information. Google finally found three solutions based on some English documents:
Solution 1: Use case-insensitive conversion functions LOWER and UPPER
Copy codeThe Code is as follows:
SELECT * FROM [User] where lower (UserName) = 'user1'
Solution 2: forcibly declare case insensitive during comparison
Copy codeThe Code is as follows:
SELECT * FROM [User] WHERE UserName = 'user1' COLLATE NOCASE
Solution 3: declare that the field is case insensitive when creating a table
Copy codeThe Code is as follows:
Create table [User] ([UserName] NVARCHAR (20) collate nocase );
If you do not need to be case sensitive under any circumstances, solution 3 is the best solution. If you only need a small number of queries that are case insensitive, use solution 2. Solution 1 is not recommended because functions are used, which may cause extra performance losses.