开发者

How to include a boolean in an SQLite WHERE clause

I get no results. How can I include a boolean conditional in a where clause in SQLite?

I have tried these

"Select * from table where col = 1"
"Select * from table where col = '1'"
"Select * from table where col = true"
"Select * from table where col = 'true'"
"Select * from table where col = 'True'"
"Select * from table where col is True"

Nothing. I even tried inclu开发者_如何学Pythonding "true" as the whereArgs in a query function.

How can I fix it?


SQLite does not have a separate Boolean storage class. Instead, Boolean values are stored as integers 0 (false) and 1 (true).

Source: SQLite

The first one then sounds correct.


SQLite version 3.7.4
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> CREATE TABLE "stack_test" ("id" INTEGER,  "bool_col" boolean);
sqlite> insert into stack_test values(1,'t');
sqlite> insert into stack_test values(2,'f');
sqlite> insert into stack_test values(3,'1');
sqlite> insert into stack_test values(4,'0');
sqlite> insert into stack_test values(5,1);
sqlite> insert into stack_test values(6,0);
sqlite> insert into stack_test values(7,banana);
Error: no such column: banana
sqlite> insert into stack_test values(7,'banana');
sqlite> .headers on
sqlite> select * from stack_test;
id|bool_col
1|t
2|f
3|1
4|0
5|1
6|0
7|banana

sqlite> select * from stack_test where bool_col=t; 
Error: no such column: t 
sqlite> select * from stack_test where bool_col='t';
id|bool_col
1|t 
sqlite> select * from stack_test where bool_col=0;
id|bool_col
4|0
6|0
sqlite> select * from stack_test where bool_col=1;
id|bool_col
3|1
5|1
sqlite> select * from stack_test where bool_col=true;
Error: no such column: true
sqlite> select * from stack_test where bool_col=banana;
Error: no such column: banana
sqlite> select * from stack_test where bool_col='banana';
id|bool_col
7|banana
sqlite>
sqlite> .schema stack_test
CREATE TABLE "stack_test" ("id" INTEGER,  "bool_col" boolean);
sqlite>


This works well "Select * from table where col = 'true'"


There is no real Boolean in SQLite. If you created it using SQLite Administrator, here is how you do it:

Select * from table where col is 'Y'
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜