Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Insert boolean value into sqlite table

I created a table MYTABLE

CREATE TABLE "MYTABLE" (
       "surname" VARCHAR,
       "name" VARCHAR,
       "id" INTEGER PRIMARY KEY  NOT NULL ,
       "flag" BOOL);

when I insert a record with:

INSERT INTO "MYTABLE" VALUES ("Super","Mario","94", true);

I get an error message, that no such column: true. If I use this:

INSERT INTO "MYTABLE" VALUES ("Super","Mario","94", "true");

I don't get any error, but when i read that record with rs.getBoolean("flag") I get false.

Finally, i tried this

INSERT INTO "MYTABLE" VALUES ("Super","Mario","94", 1); 

the rs.getBoolean("flag") returns true. So the lesson here is that the boolean values in Sqlite are inserted with 0/1 ?

like image 354
yaylitzis Avatar asked Oct 12 '15 08:10

yaylitzis


People also ask

Can you store boolean in SQLite?

Boolean Datatype. SQLite does not have a separate Boolean storage class. Instead, Boolean values are stored as integers 0 (false) and 1 (true). SQLite recognizes the keywords "TRUE" and "FALSE", as of version 3.23.

How do I add a boolean value to a database?

You can insert a boolean value using the INSERT statement: INSERT INTO testbool (sometext, is_checked) VALUES ('a', TRUE); INSERT INTO testbool (sometext, is_checked) VALUES ('b', FALSE); When you select a boolean value, it is displayed as either 't' or 'f'.

How do you set a boolean value in SQL query?

You can update boolean value using UPDATE command. If you use the BOOLEAN data type, MySQL internally convert it into tinyint(1). It can takes true or false literal in which true indicates 1 to tinyint(1) and false indicates 0 to tinyint(1).

Can we store boolean in database?

Yes, in Oracle you can store and retrieve a boolean value into a table for a column with VARCHAR2 datatype.


2 Answers

SQLite does not have a separate Boolean storage class.Boolean values are stored as integers 0 and 1. source

like image 64
Osama Aftab Avatar answered Sep 22 '22 16:09

Osama Aftab


Yes, the BOOL type is synonymous to a BIT in many databases, including SQLite and SQL Server. Other databases, like Oracle, do not even have a boolean type and a NUMBER(1) field is used to store boolean values by convention.

like image 23
Panagiotis Kanavos Avatar answered Sep 22 '22 16:09

Panagiotis Kanavos