繁体   English   中英

sqlite为什么找不到此行?

[英]Why can't sqlite find this row?

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

SELECT * FROM test WHERE word =“ id”

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

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

在此处输入图片说明

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM