SQLite区分大小写查询
来源:互联网 发布:sql 增加列 默认值 编辑:程序博客网 时间:2024/06/05 02:57
大部分数据库在进行字符串比较的时候,对大小写是不敏感的。
但是,在SQLite中,对大小写是敏感的。
假设表Test的结构和值如下:
_id name 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');
.....
查询到的记录都如下:
1 ABCDE 2 abcde 3 ABCde 4 abCDE
方案二:在进行比较时强制声明不区分大小写
select * from test where name = 'ABCDE' COLLATE NOCASE;
查询到的记录如下:
1 ABCDE 2 abcde 3 ABCde 4 abCDE
方案三:创建表时声明该字段不区分大小写
create table test (_id Integer,name Text COLLATE NOCASE );
如果在任何情况下都不需要对大小写敏感,方案三是最好的解决方案;
如果只是少量查询对大小写不敏感,可以用方案二。
而方案一由于用到了函数,可能有额外的性能消耗,不推荐使用。
<script type="text/javascript"><!--google_ad_client = "ca-pub-1944176156128447";/* cnblogs 首页横幅 */google_ad_slot = "5419468456";google_ad_width = 728;google_ad_height = 90;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
- SQLite区分大小写查询
- SQLite区分大小写查询
- sqlite查询区分大小写
- sqlite不区分大小写查询
- 查询区分大小写
- 查询区分大小写
- SQL查询区分大小写
- 查询区分大小写
- 查询区分大小写
- 查询区分大小写
- 查询区分大小写
- 查询区分大小写
- 查询区分大小写
- 查询区分大小写
- 查询区分大小写
- 查询区分大小写
- 查询区分大小写
- 查询区分大小写
- mongodb 安装与启动
- Android任务与返回堆栈 (Tasks and Back Stack)
- 深入学习XML和XSD
- libjpeg移植arm
- VC6打开一个文件或工程的时候,会导致VC6崩溃而关闭
- SQLite区分大小写查询
- jquery2.0的新特性
- SQL Server索引进阶第九篇:解读执行计划
- VC++ WIN32 sdk实现按钮自绘详解.
- 无拘束自由分享 Android传输方法总结
- C++ 快速排序QuickSort的实现
- Mysql存储java对象
- SQL Server索引进阶第十篇:索引的内部结构
- 汇编学习历程(int中断学习)