4

bit(1)在“MySQL”的类型列中插入值的正确语法是什么?

我的列定义是:

payed bit(1) NOT NULL

我正在从csv数据保存为0或的位置加载数据1。我尝试使用以下方法进行插入:

b'value' or 0bvalue (example b'1' or 0b1)

手册所示。

但我不断收到此错误:

 Warning | 1264 | Out of range value for column 'payed' at row 1

插入bit值的正确方法是什么?

我不是手动插入,而是从csv(使用load data infile)加载数据,其中列的数据是01

这是我的load查询,我已重命名隐私问题的字段,该定义没有错误:

load data local infile 'input_data.csv' into table table
fields terminated by ',' lines terminated by '\n'
(id, year, field1, @date2, @date1, field2, field3, field4, field5, field6, payed, field8, field9,   field10, field11, project_id)
set
date1 = str_to_date(@date1, '%a %b %d %x:%x:%x UTC %Y'),
date2 = str_to_date(@date2, '%a %b %d %x:%x:%x UTC %Y');
show warnings;

这是我的 CSV 的示例行:

    200014,2013,0.0,Wed Feb 09 00:00:00 UTC 2014,Thu Feb 28 00:00:00 UTC 2013,2500.0,21,Business,0,,0,40.0,0,PROSPECT,1,200013

更新: 我没有找到解决方案bit,所以我将列数据类型从 更改bittinyint以使其工作。

4

4 回答 4

13

我终于找到了解决方案,并将其发布在这里以供将来参考。我在mysql 加载数据手册页中找到了帮助。

因此,出于测试目的,我的表结构是:

+--------+-------------+------+-----+---------+-------+
| Field  | Type        | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| id     | int(11)     | NO   | PRI | NULL    |       |
| nome   | varchar(45) | YES  |     | NULL    |       |
| valore | bit(1)      | YES  |     | NULL    |       |
+--------+-------------+------+-----+---------+-------+

我的csv测试文件是:

1,primo_valore,1
2,secondo_valore,0
3,terzo_valore,1

将 加载csv到表中的查询是:

 load data infile 'test.csv' into table test
    fields terminated by ',' lines terminated by '\n'
    (id, nome, @valore) set
       valore=cast(@valore as signed);
    show warnings;

如您所见,加载csv您需要进行强制转换cast(@valore as signed),并且csv您可以使用整数表示法10指示bit值。这是因为无法使用二进制表示法(例如b'011010')加载 BIT 值。

于 2013-07-24T15:15:18.907 回答
11

用任何值替换 csv 中的“0”值。这对我有用。

于 2013-07-24T14:16:06.863 回答
1

您可以像这样使用BIN()函数:

INSERT INTO `table` VALUES (`column` = BIN(1)), (`column` = BIN(0));
于 2013-03-28T14:04:35.860 回答
0

让我猜猜,但我认为您应该在 LOAD 查询中忽略 CSV 文件的第一行。

请参阅“忽略数字行”

于 2013-03-28T14:15:50.513 回答