Case sensitivity of SQLite in string comparison

Source: Internet
Author: User

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:

Username
1 User1

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:

Username
1 User1

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.

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.