Reputation: 1543
I want to save my value as a boolean in my mysql database. But somehow Iam not able to save it , mysql automatically saves it of the type tinyInt.Also tell me the default values for boolean. how we pass the values?
Upvotes: 22
Views: 62325
Reputation: 1
I needed to hear this. I was wondering how I could set a boolean field in MYSQL.
But seeing that only tinyint is supported, I converted the Boolean values to integers in python, before inserting them into the database.
is_holiday = 0
if row[4] != 'FALSE':
is_holiday = 1
# insert a record into DB
cursor.execute("INSERT into sales(store, department, date, weekly_sales, is_holiday) VALUES(%s, %s, %s, %s, %s)", (row[0], int(row[1]), sales_date, float(row[3]), is_holiday))
Upvotes: 0
Reputation: 31740
MySQL doesn't really have a BOOLEAN type, if you create a BOOLEAN column it will actually be a TINYINT.
Treating TINYINT as a boolean isn't too problematic though, if you treat 0 as false and non-0 as true then it's fine. In PHP a statement like if ($column)
will return true if $column is any value except 0 or something that evaluates to 0. If you need it to explicitly be a bool you can convert it easily enough by doing $column = ($column != 0);
Upvotes: 8
Reputation: 62387
In MySQL BOOLEAN
type is a synonym for TINYINT
. There is no dedicated BOOLEAN
type.
The vaules accepeted, are those for TINYINT
i.e. 0 for false, 1-255 (preferably 1) for true.
Upvotes: 41