在 `MySQL' 中将值插入 bit(1)
类型的列中的正确语法是什么?
我的列定义是:
payed bit(1) NOT NULL
我正在从 csv
加载数据,其中数据保存为 0
或 1
。
我尝试使用以下方法进行插入:
b'value' or 0bvalue (example b'1' or 0b1)
如 manual 所示.
但我一直收到这个错误:
Warning | 1264 | Out of range value for column 'payed' at row 1
插入位
值的正确方法是什么?
我没有手动插入,而是从 csv
(使用 load data infile
)加载数据,其中列的数据是 0
或 1
。
这是我的加载
查询,我已经重命名了隐私问题的字段,该定义没有错误:
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
的解决方案,所以我将列数据类型从 bit
更改为 tinyint
以使其工作。
最佳答案
我终于找到了解决方案,并将其发布在这里以供将来引用。我在 mysql load data manual page 中找到了帮助.
所以出于测试目的,我的表结构是:
+--------+-------------+------+-----+---------+-------+
| 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
中可以使用整数符号1
或0
来表示位
值。这是因为 BIT 值无法使用二进制表示法加载(例如,b'011010'
)。
关于mysql - 从 mysql 中的位字段中的 CSV 加载数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15683809/