sql - 如果 Where 子句末尾有字符 `s`,则 Where 子句将搜索所有内容

标签 sql sqlite

我试图在 sqlite3 中运行一个简单的 select 命令并得到奇怪的结果。我想搜索一列并显示其中包含字符串 dockerhosts 的所有行。但结果显示行中没有 dockerhosts 字符串。

例如搜索dockerhosts:

sqlite> SELECT command FROM history WHERE command like '%dockerhosts%' ORDER BY id DESC limit 50;
git status
git add --all v1 v2
git status

如果我从末尾删除 s,我会得到我需要的:

sqlite> SELECT command FROM history WHERE command like '%dockerhost%' ORDER BY id DESC limit 50;
git checkout -b hotfix/collapse-else-if-in-dockerhost
vi opt/dockerhosts/Docker
aws s3 cp dockerhosts.json s3://xxxxx/dockerhosts.json --profile dev
aws s3 cp dockerhosts.json s3://xxxxx/dockerhosts.json --profile dev
history | grep dockerhost | grep prod
history | grep dockerhosts.json

我错过了什么?

最佳答案

我在这里看到一条注释,LIKE 模式有可配置的限制 - sqlite.org/limits.html ... 10 似乎很短,但也许这就是您遇到的问题。

The pattern matching algorithm used in the default LIKE and GLOB implementation of SQLite can exhibit O(N²) performance (where N is the number of characters in the pattern) for certain pathological cases. To avoid denial-of-service attacks from miscreants who are able to specify their own LIKE or GLOB patterns, the length of the LIKE or GLOB pattern is limited to SQLITE_MAX_LIKE_PATTERN_LENGTH bytes. The default value of this limit is 50000. A modern workstation can evaluate even a pathological LIKE or GLOB pattern of 50000 bytes relatively quickly. The denial of service problem only comes into play when the pattern length gets into millions of bytes. Nevertheless, since most useful LIKE or GLOB patterns are at most a few dozen bytes in length, paranoid application developers may want to reduce this parameter to something in the range of a few hundred if they know that external users are able to generate arbitrary patterns.

The maximum length of a LIKE or GLOB pattern can be lowered at run-time using the sqlite3_limit(db,SQLITE_LIMIT_LIKE_PATTERN_LENGTH,size) interface.

关于sql - 如果 Where 子句末尾有字符 `s`,则 Where 子句将搜索所有内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43498882/

相关文章:

python - Python 中 Sqlite 查询提供的绑定(bind)数量不正确

linux - 我是否达到了人为的 QSqlDatabase 限制?

android - sqlite 数据库在设备上的位置

java - Android SQLiteAssetHelper 的 NullPointerException

php - 如何排除三个不同表的查询结果?

sql - 三值逻辑上下文中 bool 运算符和比较运算符的语义

MySQL LIKE 关键字未返回预期结果

mysql - 选择包含一列中的值的多行

python - 如何使用 sqlite 后端为 python blaze 提供用户定义的函数?

mysql - 检查mysql中的数字是否为素数