mysql布尔值数据怎么添加,如何将BOOL值插入MySQL数据库

在尝试向MySQL数据库的BOOL类型字段插入TRUE和FALSE值时,直接使用字符串会导致插入0。原因是TRUE和FALSE是关键字,应当不加引号直接使用。正确的插入方式是不加引号并使用NULL代替空字符串。
摘要由CSDN通过智能技术生成

I am trying to insert values into a BOOL data type in MySQL (v 5.5.20) using the following script:

CREATE DATABASE DBTest;

USE DBTest;

DROP TABLE IF EXISTS first;

CREATE TABLE first (id INT AUTO_INCREMENT NOT NULL PRIMARY KEY , name VARCHAR(30) ,sale BOOL,);

INSERT INTO first VALUES ("", "G22","TRUE");

INSERT INTO first VALUES ("", "G23","FALSE");

But the INSERT statement just inserts 0 (Zero) to the boolean column for both TRUE and FALSE options! Can you please let me know why this is happening?

解决方案

TRUE and FALSE are keywords, and should not be quoted as strings:

INSERT INTO first VALUES (NULL, 'G22', TRUE);

INSERT INTO first VALUES (NULL, 'G23', FALSE);

By quoting them as strings, MySQL will then cast them to their integer equivalent

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值