2011-01-28 22 views
9

Điều này khiến tôi phát điên. Tôi cảm thấy như tôi đã thử mọi thứ và tôi không có kết quả. Làm thế nào tôi có thể bao gồm một điều kiện boolean trong một mệnh đề where trong sql lite?Làm thế nào để bao gồm một boolean trong một lite sql, nơi khoản

Tôi đã thử các

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

Không có gì. Tôi thậm chí đã thử bao gồm "true" là whereArgs trong một hàm truy vấn.

Mọi người đã làm điều này trước đây?

+1

'... nơi (col) '? –

Trả lời

11
SQLite không có lớp lưu trữ Boolean riêng biệt. Thay vào đó, các giá trị Boolean được lưu trữ dưới dạng số nguyên 0 (sai) và 1 (đúng).

Nguồn: SQLite

Âm thanh đầu tiên sau đó đúng.

+0

Tôi đã chạy trên cùng một thông tin một giờ trước. Tôi đếm các hàng nơi val là cả 0 và 1, và nó luôn luôn 0. có thể là một cái gì đó khác đang xảy ra tôi không nhìn thấy. Chỉ cần tự hỏi nếu ai đó đã làm điều này trước đây. –

+1

Có thể bạn không xem đúng giá trị. Hãy thử 'sqlite3 .dump' để kiểm tra giá trị thực của trường. – seppo0010

0

Không có Boolean thực trong SQLIte. nếu bạn tạo sử dụng SQLite Administrator, đây là cách bạn làm điều đó:

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

này hoạt động tốt "Select * from bàn nơi col = 'true'"

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> 
Các vấn đề liên quan