【问题标题】:Query for small/capital letter in PostgreSQL and SQLITE3在 PostgreSQL 和 SQLITE3 中查询小写/大写字母
【发布时间】:2019-03-20 20:08:30
【问题描述】:

无论用户在 tkinter 中使用什么大小写字母,小写还是大写,我如何从数据库中选择记录?在 MySQL 中我使用 BINARY,但 PostgreSQL 和 SQLITE 呢?

【问题讨论】:

  • 所以您希望 python 代码进行数据库查询,在该查询中您使用一个在 PostgreSQL 或 SQLITE 中可能是小写或大写的字母来选择记录?

标签: python postgresql sqlite tkinter


【解决方案1】:

在 MySQL 中,默认排序规则不区分大小写。在 SQLite 中,它是二进制的。在 PostgreSQL 中,它默认为操作系统定义的语言环境排序规则。

您可以使用lower 强制进行不区分大小写的匹配:

select a from f where lower(a) = lower('myString')

在 PostgreSQL 中,您还可以对列使用不区分大小写的数据类型,例如 citext

create table a (f citext);

另一种选择是使用忽略大小写的ilike。 (请注意,如果您想要文字匹配,则应使用 ilike 转义特殊字符。)

select f from a where f ilike 'myString';

在 SQLite 中,您可以在列定义中使用指令 collate nocase

create table a (f text collate nocase);

或者在查询中:

select f from a where f = 'myString` collate nocase;

【讨论】:

  • 感谢您的帮助!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-04-14
  • 2021-10-26
  • 2013-02-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多