We have a CSV file with thousands of records in it. I want to import these rows into a MySQL table via phpmyadmin. here is the command used:
load data local infile '/var/www/html/deansgrads_201280.csv'
into table ttu_nameslist
fields terminated by ','
enclosed by '"'
lines terminated by '\r\n'
(firstname, middlename, lastname, city, county, state, termcode, category)
There is an ID field in the table that is set to auto-increment. When we execute this SQL only the first line is imported into the table.
Input data file lines:
"Aaron","Al","Brockery","Cookeville","Putnam","TN","201280","deanslist"
"Aaron","Dan","Mickel","Lebanon","Wilson","TN","201280","deanslist"
Table structure:
CREATE TABLE `ttu_nameslist` (
`id` int(11) NOT NULL,
`firstname` varchar(50) NOT NULL,
`middlename` varchar(50) NOT NULL,
`lastname` varchar(50) NOT NULL,
`city` varchar(50) NOT NULL,
`county` varchar(50) NOT NULL,
`state` varchar(2) NOT NULL,
`termcode` varchar(6) NOT NULL,
`category` varchar(10) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1
What am I doing wrong why does it quit after adding one row?
For those interested, I had the same issue as described above but it had nothing to do with the LINES TERMINATED BY assignment, primary key violations or wrong character set.
I started getting this error after upgrading to MySQL 8 and my problem ended up being the column names I was sending via the col_name_or_user_var option. Originally I had extra columns in the import file that I wasn't using, and this caused no issue with the original implementation. After upgrading, however, I had to add variable entries for these columns to get it working and getting more than one row imported (I did not have access to change these columns in import.csv). With an import file like this:
I had to go from:
to:
You say that the ID field has the
AUTO_INCREMENT
attribute, but there's no mention of it in theCREATE TABLE
statement. This is part of the problem.The other part is those truncation warnings. Some of the rows in the CSV file probably contain data that is too long to fit inside the columns. Increase the size of those text columns to a bigger value (let's say 200) and try again.
Are you absolutely sure that the CSV file is valid ? (a.k.a. each row has the same number of values etc.). You should probably check if those strings contain commas (
,
), although that shouldn't be an issue.I had the same problem and I solved using doble quotes "\r\n" instead '\r\n'
Why is MySQL LOAD DATA INFILE command only loading one row?
What is happening is you load the first column fine, then when you load the second and it fails because is violates a unique index constraint.
MySQL
LOAD DATA LOCAL INFILE
will enforce your unique indexes on columns and then skip any offending duplicate rows without doing anything about it, not even logging a warning!How to reproduce this phenemenon:
Create a table with an int and varchar column:
Add a unique constraint on the varchar column:
Create your input text file /tmp/foo.txt, delimited by tabs, which violates the unique constraint:
Try importing it:
BAM! There's your problem: why is it only importing only one row? Because you have a unique key constraint on it which the lines in the data file violated.
Solutions:
Remove the unique key constraint on your table and try again.
Go into the text file and remove the duplicate rows that violate the unique constraint.