sql - 为什么sqlite找不到这一行?

标签 sql sqlite

我正在使用 sqlite3。我的测试表包含两行(见屏幕转储图像),但我无法使用

找到第二行

SELECT * FROM test WHERE word="id"

声明。为什么sqlite找不到那一行?

(我认为问题在于一个属性也称为“id”,因为我发现如果将该属性命名为 id2,则 select 语句将起作用。)

enter image description here

最佳答案

这是因为双引号,使用单引号可以正常工作:

sqlite> create table test ( id integer primary key autoincrement, word text not null unique on conflict ignore );
sqlite> insert into test values (1, 'xxx');
sqlite> insert into test values (2, 'id');
sqlite> select * from test;
1|xxx
2|id
sqlite> select * from test where word = "id";
sqlite> select * from test where word = 'id';
2|id

这是因为 id 是当前模式中列的名称,因为您可以在列名称周围使用双引号,sqlite 认为您在谈论列,而不是给出字符串。即:

sqlite> insert into test values (3, 3);
sqlite> select * from test where word = "id";
3|3
sqlite> select * from test where word = id;
3|3

如果您可以在列上使用双引号,那是因为列名中的空格是合法的:

sqlite> create table test2 ( "foo bar" integer );
sqlite> .schema test2
CREATE TABLE test2 ( "foo bar" integer );
sqlite> insert into test2 values (42);
sqlite> select * from test2;
42
sqlite> select * from test2 where "foo bar" = 42;
42

关于sql - 为什么sqlite找不到这一行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23553857/

相关文章:

sql - 有没有一种方法可以选择SQL中的成对列表中的行?

python - 如何将整个 sqlite3 查询放到 tkinter 页面上

c# - 如何组合在 C# 中运行的两个 SQLite 语句?

java - 从列名包含空格的 SQLite 中读取数据

c# - 以编程方式使用转储文件创建sqlite数据库

php - 从跨越多天的数据库中获取事件

sql - 如何将此 native SQL 查询转换为 HQL

sql - 我想以分层形式生成 XML 文件

c# - System.EntryPointNotFoundException + 无法安装包 'SQLite.Interop.dll 1.0.103' 。我的项目目标'.NETFramework,Version=v4.6.1'

mysql - SQL结果加法