2011-01-28 71 views
9

这让我疯狂。我觉得我已经尝试了一切,但没有结果。如何在sql lite中的where子句中包含布尔条件?如何将一个布尔值包含在sql lite子句中

我尝试了这些

"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" 

没有。我甚至尝试在查询函数中包含“true”作为whereArgs。

以前有人做过这个吗?

+1

`...其中(COL )`? – 2011-01-28 03:59:05

回答

11
SQLite没有单独的布尔存储类。相反,布尔值被存储为整数0(假)和1(真)。

来源:SQLite

第一个就会发出正确的。

+0

我在一小时前跑过相同的信息。我正在计算val是0还是1的行,并且它总是0.可能是其他事情正在发生,我没有看到。只是想知道如果有人以前做过这个。 – 2011-01-28 04:13:12

0

SQLIte中没有真正的布尔值。如果您创建使用SQLite管理员,这里是你如何做到这一点:

Select * from table where col is 'Y' 
-1

这行之有效 “选择从表*其中列= '真'”

1
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> 
相关问题