How to solve the case-sensitivity problem when comparing SQLite strings. sqlite strings are case-insensitive.

Source: Internet
Author: User

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:

  UserName
1 User1

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.