Code: Select all
drop table if exists test;
create table test (col1 VARCHAR(64), col2 VARCHAR(80) );
LOAD DATA LOCAL INFILE "t.csv" INTO TABLE test FIELDS OPTIONALLY ENCLOSED BY '"' ESCAPED BY '"' COLUMNS TERMINATED BY ';' LINES TERMINATED BY '\r\n' ;
< /code>
Die Daten sind Folgendes: < /p>
E507;^M"A string^M
spanning several lines "some Umlauts in "üöä" to show"^M
escaping^M
last line"^M
E600;"once^M
again"^M
< /code>
Der Fehler tritt auf, wenn ich die zugeordneten Schlüsselwörter der Felder hinzufüge:
FIELDS OPTIONALLY ENCLOSED BY '"' ESCAPED BY '"'
Absicht dahinter ist, dass Varchars entweder ohne '"' und Varchars in '"' gerahmt sind. Aber die äußeren Framing '' sollten nicht in den Daten erscheinen.
Code: Select all
E605;
E507;"A string
spanning several lines "some Umlauts in ""üöä"" to show""
escaping
last line"
E600;"once
again"
< /code>
als hexdump: < /p>
00000000 45 36 30 35 3b 0d 0a 45 35 30 37 3b 22 41 20 73 |E605;..E507;"A s|
00000010 74 72 69 6e 67 0d 0a 73 70 61 6e 6e 69 6e 67 20 |tring..spanning |
00000020 73 65 76 65 72 61 6c 20 6c 69 6e 65 73 20 22 22 |several lines ""|
00000030 73 6f 6d 65 20 55 6d 6c 61 75 74 73 20 69 6e 20 |some Umlauts in |
00000040 22 22 c3 bc c3 b6 c3 a4 22 22 20 20 74 6f 20 73 |""......"" to s|
00000050 68 6f 77 22 22 0d 0a 65 73 63 61 70 69 6e 67 22 |how""..escaping"|
00000060 22 0d 0a 6c 61 73 74 20 6c 69 6e 65 22 0d 0a 45 |"..last line"..E|
00000070 36 30 30 3b 22 6f 6e 63 65 0d 0a 61 67 61 69 6e |600;"once..again|
00000080 22 0d 0a |"..|
00000083
< /code>
Mit dem folgenden SQL erhalte ich die gewünschten Ergebnisse: < /p>
drop table if exists test;
create table test (col1 TEXT, col2 TEXT);
LOAD DATA LOCAL INFILE "t.csv" INTO TABLE test FIELDS TERMINATED BY ';' OPTIONALLY ENCLOSED BY '"' LINES TERMINATED BY
'\r\n' ;
select * from test;
mysql> source loadtest.sql
Query OK, 0 rows affected (0.01 sec)
Query OK, 0 rows affected (0.04 sec)
Query OK, 3 rows affected (0.01 sec)
Records: 3 Deleted: 0 Skipped: 0 Warnings: 0
+------+-----------------------------------------------------------------------------------------+
| col1 | col2 |
+------+-----------------------------------------------------------------------------------------+
| E605 | |
| E507 | A string
spanning several lines "some Umlauts in "üöä" to show"
escaping"
last line |
| E600 | once
again |
+------+-----------------------------------------------------------------------------------------+
3 rows in set (0.00 sec)
mysql>