为什么我的数据会被截断?警告| 1265 |列的数据被截断

时间:2015-05-02 23:36:49

标签: mysql date null warnings truncated

我尝试在date_of_birth和date_of_death中添加NULL,但这没有任何帮助。为什么数据会被截断? (特别是因为目前还没有任何数据......)

另外,我根本不理解警告1261。第5行与第1-4行没有区别。

谢谢!

以下脚本:

-- create the people table
DROP TABLE IF EXISTS people;

CREATE TABLE people (
full_name varchar (50) NOT NULL,
people_ID varchar (15) NOT NULL,
date_of_birth DATE,
date_of_death DATE,
PRIMARY KEY(people_ID))
ENGINE = INNODB;

LOAD DATA LOCAL INFILE "People.csv"
INTO TABLE people
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n'
IGNORE 1 LINES;  

给我这些错误:

Query OK, 5 rows affected, 10 warnings (0.00 sec)
Records: 5  Deleted: 0  Skipped: 0  Warnings: 10

mysql> show warnings;
+---------+------+----------------------------------------------------+
| Level   | Code | Message                                            |
+---------+------+----------------------------------------------------+
| Warning | 1265 | Data truncated for column 'date_of_birth' at row 1 |
| Warning | 1265 | Data truncated for column 'date_of_death' at row 1 |
| Warning | 1265 | Data truncated for column 'date_of_birth' at row 2 |
| Warning | 1265 | Data truncated for column 'date_of_death' at row 2 |
| Warning | 1265 | Data truncated for column 'date_of_birth' at row 3 |
| Warning | 1265 | Data truncated for column 'date_of_death' at row 3 |
| Warning | 1265 | Data truncated for column 'date_of_birth' at row 4 |
| Warning | 1265 | Data truncated for column 'date_of_death' at row 4 |
| Warning | 1265 | Data truncated for column 'date_of_birth' at row 5 |
| Warning | 1261 | Row 5 doesn't contain data for all columns         |
+---------+------+----------------------------------------------------+
10 rows in set (0.00 sec)

mysql> select * from people;
+-------------------+-----------+---------------+---------------+
| full_name         | people_ID | date_of_birth | date_of_death |
+-------------------+-----------+---------------+---------------+
| Harry Dunham      | H_Dunham  | 0000-00-00    | NULL          |
| Julien Bryan      | J_Bryan   | 0000-00-00    | 0000-00-00    |
| Jules V.D. Bucher | J_Bucher  | 0000-00-00    | 0000-00-00    |
| Miriam Bucher     | M_Bucher  | 0000-00-00    | 0000-00-00    |
| Paul Bowles       | P_Bowles  | 0000-00-00    | 0000-00-00    |
+-------------------+-----------+---------------+---------------+
5 rows in set (0.01 sec)

这是我的.csv:

FullName,People_ID,DOB,DOD
Jules V.D. Bucher,J_Bucher,,
Miriam Bucher,M_Bucher,,
Julien Bryan,J_Bryan,,
Paul Bowles,P_Bowles,,
Harry Dunham,H_Dunham,,

2 个答案:

答案 0 :(得分:4)

也许正在发生的事情是Mysql试图强制将空字符串""添加到您的日期列中,但该列只接受DATE格式化的数据,因此它被转换为适当的格式。尝试调整您的CSV,以便其中一行在日期列中有"0000-00-00"而不是""(空白),并查看您是否收到的警告更少。

1261警告看起来更像是相同的 - 可能是无害的,但是在Mysql如何处理CSV导入方面令人讨厌的怪癖 - 除非您看到导入的数据不完整的证据,否则我不会担心

答案 1 :(得分:4)

总而言之,我已经确定您的CSV文件包含DOS样式的行结尾(\n),而您已指示MySQL使用UNIX样式的行结尾来解释该文件(\r

这些\r个字符因此被视为数据,DATE在字符串到LOAD DATA LOCAL INFILE "People.csv" INTO TABLE people FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\r\n' IGNORE 1 LINES; 转换期间被“截断”为虚无,最后一行确实不包含任何数据最后一列的任何内容(因为你没有在CSV文件的末尾写一个空行)。这就是最后一行的不同之处。

您实际上可以fix this by using a more appropriate LINES TERMINATED BY directive

\r\n
然后,MySQL将允许将"序列视为行尾。

你还有另一个问题,就是你告诉MySQL你的字段被ENCLOSED BY括起来了,而事实并非如此。您可以删除// Display Fields add_action( 'woocommerce_product_options_general_product_data', 'woo_add_custom_general_fields' ); // Save Fields add_action( 'woocommerce_process_product_meta', 'woo_add_custom_general_fields_save' ); function woo_add_custom_general_fields() { global $woocommerce, $post; echo '<div class="options_group">'; // Custom fields will be created here... // Text Field woocommerce_wp_text_input( array( 'id' => '_ISBN_field', 'label' => __( 'ISBN', 'woocommerce' ), 'placeholder' => '', 'desc_tip' => 'true', 'description' => __( 'ISBN.', 'woocommerce' ) ) ); function woo_add_custom_general_fields_save( $post_id ){ // Customer text ISBN Field $woocommerce_text_field = $_POST['_ISBN_field']; if( !empty( $woocommerce_text_field ) ) update_post_meta( $post_id, '_ISBN_field', esc_attr( $woocommerce_text_field ) ); else update_post_meta( $post_id, '_ISBN_field', '' ); } // Display Custom Field Value echo get_post_meta( $post->ID, '_ISBN_field', true ); } /* WooCommerce */ /* ----------------------------------------------------------------------------------- */ /* Start WooThemes Functions - Please refrain from editing this section */ /* ----------------------------------------------------------------------------------- */ 指令or precede it with OPTIONALLY

  

如果输入值不一定用引号括起来,请在ENCLOSED BY关键字之前使用OPTIONALLY。

您应该阅读您使用的技术的文档。