Вопрос

I am using sqlite3. My test table contains two rows (see screen dump image), but I can not find the second row using the

SELECT * FROM test WHERE word="id"

statement. Why can't sqlite find that row?

(I think the problem is that one attribute is also called "id", because I have found that the select statement would work if that attribute where named id2 instead.)

enter image description here

Это было полезно?

Решение

this is because of the double quotes, using single quotes will have it work fine:

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

and this is because id is the name of a column in your current schema, as you can use double quotes around column names, sqlite thinks you're talking about columns, not giving a string. i.e.:

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

if you can use double quotes on columns, it's because spaces are legal in column names:

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 с атрибуция
Не связан с StackOverflow
scroll top