Author:Heero
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:
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 applyProgramCause adverse effects. 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
Select * from [user] WhereLower (username)= 'User1'
Solution 2: forcibly declare case insensitive during comparison
Select * from [user] Where username = 'user1'Collate nocase
Solution 3: declare that the field is case insensitive when creating a table
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.
Author:Heero
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:
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
Select * from [user] WhereLower (username)= 'User1'
Solution 2: forcibly declare case insensitive during comparison
Select * from [user] Where username = 'user1'Collate nocase
Solution 3: declare that the field is case insensitive when creating a table
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.