2017-01-12 6 views
0

Ich habe eine CSV-Datei mit einer Spalte, die eine durch Kommas getrennte Liste von Werten enthalten kann.Importieren einer CSV-Datei in mySQL

Ich habe die folgende SQL-Anweisung, um die Daten zu importieren:

LOAD DATA LOCAL INFILE 'C:/tmp/geography.csv' 
INTO TABLE geography 
FIELDS TERMINATED BY ',' 
ENCLOSED BY '"' 
IGNORE 1 ROWS; 

Wenn es jedoch eine Reihe erreicht, die eine durch Kommata getrennte Liste von Werten enthält, das den Import zu brechen.

Das Format der Daten wie folgt aussieht:

id,zip,location_type,primary_city,state,county,area_codes 
    ,00501,UNIQUE,Holtsville,NY,Suffolk County,631 
    ,00601,STANDARD,Adjuntas,PR,Adjuntas Municipio,"787,939" 

enter image description here

Antwort

0

ich das Problem nicht reproduzieren kann.

Datei: /path/to/file/geography.csv

id,zip,location_type,primary_city,state,county,area_codes 
\N,00501,UNIQUE,Holtsville,NY,Suffolk County,631 
\N,00601,STANDARD,Adjuntas,PR,Adjuntas Municipio,"787,939" 
\N,00602,STANDARD,Aguada,PR,Aguada Municipio,"797,949" 

MySQL-Kommandozeilen:

mysql> SELECT VERSION(); 
+-------------------------+ 
| VERSION()    | 
+-------------------------+ 
| 5.7.16-0ubuntu0.16.04.1 | 
+-------------------------+ 
1 row in set (0.00 sec) 

mysql> DROP TABLE IF EXISTS `geography`; 
Query OK, 0 rows affected (0.01 sec) 

mysql> CREATE TABLE IF NOT EXISTS `geography` (
    -> `id` INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, 
    -> `zip` VARCHAR(5) NOT NULL, 
    -> `location_type` VARCHAR(25) NOT NULL, 
    -> `primary_city` VARCHAR(25) NOT NULL, 
    -> `state` VARCHAR(25) NOT NULL, 
    -> `county` VARCHAR(25) NOT NULL, 
    -> `area_codes` VARCHAR(25) NOT NULL 
    ->); 
Query OK, 0 rows affected (0.00 sec) 

mysql> LOAD DATA LOCAL INFILE '/path/to/file/geography.csv' 
    -> INTO TABLE `geography` 
    -> FIELDS TERMINATED BY ',' ENCLOSED BY '"' 
    -> IGNORE 1 ROWS; 
Query OK, 3 rows affected (0.00 sec) 
Records: 3 Deleted: 0 Skipped: 0 Warnings: 0 

mysql> SELECT 
    -> `id`, 
    -> `zip`, 
    -> `location_type`, 
    -> `primary_city`, 
    -> `state`, 
    -> `county`, 
    -> `area_codes` 
    -> FROM 
    -> `geography`; 
+----+-------+---------------+--------------+-------+--------------------+------------+ 
| id | zip | location_type | primary_city | state | county    | area_codes | 
+----+-------+---------------+--------------+-------+--------------------+------------+ 
| 1 | 00501 | UNIQUE  | Holtsville | NY | Suffolk County  | 631  | 
| 2 | 00601 | STANDARD  | Adjuntas  | PR | Adjuntas Municipio | 787,939 | 
| 3 | 00602 | STANDARD  | Aguada  | PR | Aguada Municipio | 797,949 | 
+----+-------+---------------+--------------+-------+--------------------+------------+ 
3 rows in set (0.00 sec) 
Verwandte Themen