I am getting all data imported in MySQL from CSV file in the first column. All other columns gets NULL values

随声附和 提交于 2019-12-06 10:54:52

Test schema:

-- drop table if exists aT;
create table aT
(   -- address thing
    COMPANY_ADDRESS1 varchar(100) not null,
    COMPANY_ADDRESS2 varchar(100) not null,
    COMPANY_CITY varchar(100) not null,
    COMPANY_COUNTRY varchar(100) not null,
    COMPANY_FAX varchar(100) not null,
    COMPANY_GERMANY varchar(100) not null,
    COMPANY_ID varchar(100) not null,
    COMPANY_INDIANAPOLIS varchar(100) not null,
    COMPANY_NAME varchar(100) not null,
    COMPANY_STATE varchar(100) not null,
    COMPANY_TELEPHONE varchar(100) not null,
    COMPANY_VAT varchar(100) not null,
    COMPANY_ZIP varchar(100) not null
);

Command run interactively or via a program:

LOAD DATA INFILE 'c:\\nate\\aT.csv' INTO TABLE aT
  FIELDS TERMINATED BY ',' ENCLOSED BY '"'
  LINES TERMINATED BY '\r\n'
  IGNORE 1 LINES;
select * from aT;

or Linux:

LOAD DATA INFILE '/home/nate/aT.csv' INTO TABLE aT
  FIELDS TERMINATED BY ',' ENCLOSED BY '"'
  LINES TERMINATED BY '\n'
  IGNORE 1 LINES;

No problem bringing the data in

select * from aT; -- view the import

Now for command line (create a .sql of one of the above):

truncate aT; -- run this first

mysql -u root -p so_gibberish2 < c:\nate\run_this1.sql

so_gibberish2 is the database name. Data comes in. Because the mysql client is not subject to bash or other command line escape sequences.

I think mysqlimport is looking for a backslash double quote to enclose fields, and it's not finding any, so it's all one field.

I'm questioning the purpose of the backslash character before the double quote. If this is for Linux bash shell, I don't think the backslash is required to specify the double quote. As a demo:

# echo foo --fields-enclosed-by='\"'
foo --fields-enclosed-by=\"

# echo foo --fields-enclosed-by='"'
foo --fields-enclosed-by="

(This is just a guess, I could be wrong. I've been wrong plenty times before. It's possible that mysqlimport is discarding that backslash character that's being passed to it. The issue might be something else. But for a test, I'd give it whirl without the backslash, and see how big of a smokeball it makes without it.)

Demo environment:

# uname -o -r -s
Linux 3.13.0-83-generic GNU/Linux

# echo $SHELL
/bin/bash
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!