简体   繁体   中英

MySQL issue when importing specific CSV files with blank values in random rows

I had brought this up before earlier, but after doing some research, I realized I was looking in the wrong place. Here is the situation. I create this table:

CREATE TABLE PC_Contacts
(
POC VARCHAR(255) PRIMARY KEY NOT NULL,
Phone_1 VARCHAR(255),
Phone_2 VARCHAR(255)
);

I import a CSV file into MySQL which has the values for my table PC_Contacts:

USE Network
LOAD DATA INFILE 'C:\\ProgramData\\MySQL\\MySQL Server 5.7\\Uploads\\PC_Contacts.csv'
INTO Table PC_Contacts
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n'
IGNORE 1 ROWS;

My output after importing looks like this:

+------------------+--------------+---------------+
| POC              | Phone_1      | Phone_2       |
+------------------+--------------+---------------+
 |April Wilson    | 123-456-5000 | 123-456-5006
             |     | 123-456-2222 |
             |     | 123-456-5331 |
             |     | 123-456-7772 |
 |Anton Watson  | 123-456-1258 | 123-456-6005
 |Elisa Kerring   | 123-456-1075 | 123-456-4475

Now as you may recall, based on my code input, that POC is the PK. I had, in the original CSV file, a value for every line. However, as you see, anything that has no value on the right affects the left column's values. However, if I looked in the GUI and pulled up the table there, it showed the cell as populated with the value, so the data is there. If I were to put in xxx-xxx-xxxx, it would fix the issue:

    +------------------+--------------+---------------+
    | POC              | Phone_1      | Phone_2       |
    +------------------+--------------+---------------+
     |April Wilson    | 123-456-5000 | 123-456-5006
     |Nicky Nite        | 123-456-2222 | xxx-xxx-xxxx
     |Nicole           | 123-456-5331 | xxx-xxx-xxxx
     |Becky            | 123-456-7772 | xxx-xxx-xxxx
     |Anton Watson  | 123-456-1258 | 123-456-6005
     |Elisa Kerring   | 123-456-1075 | 123-456-4475

Obviously my intentions are so that I can see the value without having to apply special formatting in the command line. Is there a special SELECT command for that maybe?

Here is a link to a portion of the .CSV, as requested:

https://drive.google.com/file/d/0B0MMqHN75RpGdkZhcGp0SWtmams/view?usp=sharing

Your CSV file contains a carriage return with newline at the end of row, which breaks formatting. Use:

SELECT POC, Phone_1, REPLACE(Phone_2, '\r', '') AS Phone_2 FROM PC_Contacts;

Or change your import query as follows:

USE Network
LOAD DATA INFILE 'C:\\ProgramData\\MySQL\\MySQL Server 5.7\\Uploads\\PC_Contacts.csv'
INTO Table PC_Contacts
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\r\n'
IGNORE 1 ROWS;

And use simple SELECT :

SELECT * FROM PC_Contacts;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM