MySQL BIT類型字段插入不成功的問題

今天有一個表的一個字段的類型爲BIT(1),我準備往裏插入一個值‘0’,但是MySQL一直提示“Data too long for column XXX”,解決辦法很簡單:

原文地址:http://www.heidisql.com/forum.php?t=7897

The MySQL manual does not seem to provide any usage example but it explains BIT among the numeric data types:

http://dev.mysql.com/doc/refman/5.1/en/numeric-type-overview.html

So we can assume it can be handled as a number. And given it's a bit field, it's likely than hexadecimal notation will make things easier. So I guess you can:

- Use hexadecimal notation to insert data (optional)
- Use the HEX() function to read the column (mandatory)

An example:

CREATE TABLE `test` (
`id` INT(11) NULL DEFAULT NULL,
`is_winter` BIT(1) NULL DEFAULT NULL
)
ENGINE=InnoDB;

insert into test (id, is_winter) values
(1, 0x0),
(2, 0x1),
(3, 0x2); -- This should be invalid

select id, hex(is_winter)
from test;



And we get:

"id";"hex(is_winter)"
"1";"0"
"2";"1"
"3";"1"




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章