我在将csv文件加载到mysql时遇到问题

时间:2019-05-17 05:33:37

标签: mysql csv mysqli mysql-workbench

基本上我想将csv文件加载到两个mysql表中,其中一个表中只有特定字段,而另一个表中有一些其他字段

这些是表定义-https://imgur.com/c7Scdkm 这是我要加载的文件-http://s000.tinyupload.com/index.php?file_id=19140190054505912011

当我运行此命令=

load data infile "C:/ProgramData/MySQL/MySQL Server 8.0/Uploads/careerbuilder_dataanalytics.csv" into table dataanalyst_ncr FIELDS TERMINATED BY ',' IGNORE 1 ROWS(@col1,@col2,@col3,@col4) set Job_title=@col3,Company_name=@col1,links=@col4,job_desc=@col2;

它显示此错误-

Error Code: 1262. Row 1 was truncated; it contained more data than there were input columns

我想将文件的每一列加载到表中的特定列

1 个答案:

答案 0 :(得分:0)

u标头后-数据前(删除)在CSV中有一个空行。 您必须形成两个语句才能将数据加载到两个表中 要加载数据,您必须使用以下命令启动mysql

mysql -u root -p --local-infile=1

文字代码有效

load data local infile '/home/infiniti/Downloads/careerbuilder_dataanalytics.csv' into table dataanalyst_ncr
fields terminated by ','
lines terminated by '\n'
ignore 1 lines
(@col1, @col2, @col3, @col4, @col5, @col6)
set 
job_title = @col3, company_name = @col1, job_desc = @col2, job_id = 0, links=@col4
mysql> create table dataanalyst_ncr ( job_title text, company_name text, links text, job_desc text, job_id int primary key auto_increment, last_updated_on timestamp) ;
Query OK, 0 rows affected (0.58 sec)


mysql> desc dataanalyst_ncr;
+-----------------+-----------+------+-----+---------+----------------+
| Field           | Type      | Null | Key | Default | Extra          |
+-----------------+-----------+------+-----+---------+----------------+
| job_title       | text      | YES  |     | NULL    |                |
| company_name    | text      | YES  |     | NULL    |                |
| links           | text      | YES  |     | NULL    |                |
| job_desc        | text      | YES  |     | NULL    |                |
| job_id          | int(11)   | NO   | PRI | NULL    | auto_increment |
| last_updated_on | timestamp | YES  |     | NULL    |                |
+-----------------+-----------+------+-----+---------+----------------+
6 rows in set (0.06 sec)

mysql> load data local infile '/home/infiniti/Downloads/careerbuilder_dataanalytics.csv' into table dataanalyst_ncr
    -> fields terminated by ','
    -> lines terminated by '\n'
    -> ignore 1 lines
    -> (@col1, @col2, @col3, @col4, @col5, @col6)
    -> set 
    -> job_title = @col3, company_name = @col1, job_desc = @col2, job_id = 0, links=@col4;
Query OK, 1 row affected (0.14 sec)
Records: 1  Deleted: 0  Skipped: 0  Warnings: 0

mysql> select * from dataanalyst_ncr;
+-----------------------------------+------------------+-------------------------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------+--------+-----------------+
| job_title                         | company_name     | links                                                       | job_desc                                                                                                                                               | job_id | last_updated_on |
+-----------------------------------+------------------+-------------------------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------+--------+-----------------+
| Manager  Field Operations - India | Hilton Corporate | /jdp/manager -field-operations----india-j3r1tw77rcl1lzd7wxb | A national Procurement Manager  Field Operations  India will assist in the planning  organization  direction  and control of the purchasing and sup... |      1 | NULL            |
+-----------------------------------+------------------+-------------------------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------+--------+-----------------+
1 row in set (0.00 sec)

mysql> 
mysql> 

相关问题