I want to import an .csv file into MySQL Database by:
load data local infile 'C:\\Users\\t_lichtenberger\\Desktop\\tblEnvironmentLog.csv'
into table tblenvironmentlog
character set utf8
fields terminated by ';'
lines terminated by '\n'
ignore 1 lines;
But I am getting the following error and I cannot explain why:
Error Code: 1300. Invalid utf8 character string: 'M'
Any suggestions?
Just open the csv file in your text editor (like Nodepad++)
and change the file Encoding to UTF-8
then import your csv file
It's complaining about
'M'
but I think it's inMünchen
and the actual problematic character is next one, the umlaut'ü'
.One simple way to test would be to try loading a file with just the first 2 rows & see if that works. Then add the 3rd row, try again & see if that fails.
If you can't or don't want to replace these special characters in your data, then you'll need to start investigating the character sets configured in your CSV file, database, table, columns, tools etc...
Are you using MySQL 5.7 or above? Then something simple to try would be to change to
character set utf8mb4
in yourload data
command.See How MySQL 5.7 Handles 'utf8mb4' and the Load Data Infile for a similar issue.
Also see:
import geonames allCountries.txt into MySQL 5.7 using LOAD INFILE - ERROR 1300 (HY000)
Trouble with utf8 characters; what I see is not what I stored
“Incorrect string value” when trying to insert UTF-8 into MySQL via JDBC?
Nothing else I tried worked for me, including ensuring that my .csv was saved with UTF-8 encoding.
This worked:
When using
LOAD DATA LOCAL INFILE
, setCHARACTER SET latin1
instead ofCHARACTER SET utf8mb4
as shown in https://dzone.com/articles/mysql-57-utf8mb4-and-the-load-data-infileHere is a full example that worked for me:
See what the settings for the export were. Look for "UTF-8".
This suggests that "Truncated text" is caused by the data not being encoded as utf8mb4. Outside MySQL, "look for "UTF-8". (Inside, MySQL, utf8 and utf8mb4 work equally well for all European character sets, so the
ü
should not be a problem.If it was exported as "cp1252" (or any of a number of encodings), the byte for
ü
would not be valid for utf8mb4, leading to truncation.If this analysis is correct, there are two solutions:
Plan A: Export as
UTF-8
.Plan B: Import as
latin1
. (You do not need to change the column/table definition, just theLOAD DATA
.)