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"




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值