我正在尝试将CSV文件导入MySQL表格,但是我遇到了各种麻烦。这就是我想要做的事情:
我正在制作一个视频数据库,并且已经有一个已经包含数据的现有表格,名为剧集。以下是它的设置方式:
+--------------+-----------------------+------+-----+-------------------+-----------------------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+-----------------------+------+-----+-------------------+-----------------------------+
| title | varchar(40) | NO | MUL | NULL | |
| media_id | varchar(11) | NO | | NULL | |
| ep_info | varchar(75) | YES | | NULL | |
| air_date | varchar(20) | NO | | NULL | |
| trt | varchar(8) | NO | | NULL | |
| times_played | mediumint(9) unsigned | NO | | 0 | |
| last_played | timestamp | YES | | NULL | |
| entered | timestamp | NO | | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| ep_desc | varchar(300) | NO | | NULL | |
+--------------+-----------------------+------+-----+-------------------+-----------------------------+
主键是id字段,title字段设置为show table的外键。节目表如下所示:
+-------------+-------------+------+-----+------------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------+-------------+------+-----+------------+-------+
| title | varchar(50) | NO | PRI | NULL | |
| title_image | varchar(50) | NO | | NULL | |
| gif_image | varchar(50) | NO | | NULL | |
| info_url | varchar(30) | NO | | shows.html | |
+-------------+-------------+------+-----+------------+-------+
我的CSV文件格式如下:
"Big Wolf On Campus","BWOC0102","Season 1 Episode 2: The Bookmobile","April 9, 1999";"21:57",NULL,NULL,NULL,NULL,"Once every 70 years, a window of transference opens that offers Tommy a chance to pass his curse to another person. Merton volunteers but that same day a bookmobile shows up in Pleasantville and people start disappearing."
"Big Wolf On Campus","BWOC0103","Season 1 Episode 3: Butch Comes To Shove","April 16, 1999","21:06",NULL,NULL,NULL,NULL,"When a character from a 1950s educational film gets sick of the rules he decides to leave the movie for Pleasantville. While there Butch decides to find someone to bring back to his black-and-white world - and Stacey is at the top of his list."
在导入期间,我希望将CSV中的数据添加到表中的现有数据中。我还希望将last_played字段设置为NULL(仅在显示播放时更新),输入的字段设置为当前时间戳,并且id字段auto_incremented与表的下一个值。
这是我的导入声明:
LOAD DATA INFILE 'ytv.csv' INTO TABLE episodes
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n';
生成的错误消息:
ERROR 1406 (22001): Data too long for column 'air_date' at row 1
我在这里做错了什么?看起来数据在导入时会在一列上移动(这样来自CSV的ep_info进入air_date列),但我无法弄清楚原因。对于这个MySQL新手,我们非常感谢任何见解。
答案 0 :(得分:0)
您似乎在show table中有一些没有映射条目的新剧集。您可以创建一个新的表,如剧集,删除任何约束,将数据加载到新表,将所有缺少的节目标题插入到您的节目表,然后将剧集从新表插入剧集表。
或者你可以删除外键,加载数据,修改显示表,然后再添加外键。