加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

SQLite区分大小写查询

发布时间:2020-12-12 20:18:58 所属栏目:百科 来源:网络整理
导读:大部分数据库在进行字符串比较的时候,对大小写是不敏感的。 但是,在SQLite中,对大小写是敏感的。 假设表Test的结构和值如下: _id name 1 ABCDE 2 abcde 3 ABCde 4 abCDE 5 aaaaa 6 bbbbb 执行下面的SQL语句: select * from test where name = 'Abcde';
_idname 1 ABCDE 2 abcde 3 ABCde 4 abCDE 5 aaaaa 6 bbbbb


执行下面的SQL语句:

select * from test where name = 'Abcde';

结果是没有查询到任何记录。

明显地,SQLite在进行字符串比较的时候,默认对大小写是敏感的

那么SQLite怎么区分大小写查询呢,以下是三种解决方案:

方案一:使用大小写转换函数LOWER、UPPER

1.select * from test where LOWER(name) = 'abcde';

2.select * from test where LOWER(name) = LOWER('ABCDE');

3.select * from test where LOWER(name) = LOWER('Abcde');

4.select * from test where LOWER(name) = LOWER('ABCde');

....

(1).select * from test where UPPER(name) = 'ABCDE';

(2).select * from test where UPPER(name) = UPPER('ABCDE');

(3).select * from test where UPPER(name) = UPPER('Abcde');

(4).select * from test where UPPER(name) = UPPER('ABCde');

.....

查询到的记录都如下:

abCDE

方案二:在进行比较时强制声明不区分大小写

select * from test where name = 'ABCDE'COLLATE NOCASE;

查询到的记录如下:

abCDE

方案三:创建表时声明该字段不区分大小写

create table test (_id Integer,name TextCOLLATE NOCASE);

如果在任何情况下都不需要对大小写敏感,方案三是最好的解决方案;

如果只是少量查询对大小写不敏感,可以用方案二。

而方案一由于用到了函数,可能有额外的性能消耗,不推荐使用。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

大部分数据库在进行字符串比较的时候,对大小写是不敏感的。
但是,在SQLite中,对大小写是敏感的。

假设表Test的结构和值如下:

    推荐文章
      热点阅读