Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't sqlite find this row?

Tags:

sql

sqlite

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

like image 769
ragnarius Avatar asked Oct 01 '22 20:10

ragnarius


1 Answers

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
like image 144
zmo Avatar answered Oct 11 '22 12:10

zmo