Question

I looked for a similar question but I could not find a magic answer....so I'm hoping I'll find one now as this is driving me crazy!

I was going great guns with importing CSV data into MySQL...and now suddenly I'm getting nothing but skipped records. I ought to mention that I have been doing lots of deleting records (just experimenting really as I'm learning about MySQL and building my first database) so I've been chopping and changing the data including lots of copy/pasting in Excel, just in case any of this may be causing the trouble.

An example here, where I chopped the CSV down to just 2 records:

GENRE_NAME
Classic Rock
Electronica

The query to load the data:

LOAD DATA INFILE 'Music_All_2.csv' IGNORE INTO TABLE genres COLUMNS TERMINATED BY ',' ESCAPED BY '\\' LINES TERMINATED BY '\r' IGNORE 1 LINES (genre_name);
Query OK, 0 rows affected (0.00 sec)
Records: 2  Deleted: 0  Skipped: 2  Warnings: 0

And the table is empty (so it's not skipping them due to them already existing):

select * from genres;
Empty set (0.00 sec)

Finally, here's the SHOW CREATE TABLE genres output:

genres | CREATE TABLE `genres` (
`genre_pk` tinyint(2) unsigned NOT NULL AUTO_INCREMENT,
 `genre_name` varchar(90) NOT NULL,
  PRIMARY KEY (`genre_pk`),
 UNIQUE KEY `genre_name` (`genre_name`)
) ENGINE=InnoDB AUTO_INCREMENT=255 DEFAULT CHARSET=latin1 |

Thank you in advance to the solver of my problem, there is sooooo much I don't know - it's fun learning but frustrating at the same time!

Was it helpful?

Solution

I think you have 2 problems.

1. AUTO_INCREMENT value

In your DDL,

  • genre_pk is TINYINT. So genre_pk can hold 0~255.
  • in end of CREATE STATEMENT, AUTO_INCREMENT=255
    • This sets initial value for genre_pk to 255

2. LINES TERMINATED BY

You need to change LINES TERMINATED BY from '\r' to '\n'

3. Summary

I've tested following SQL, and it worked well.

DDL

CREATE TABLE `genres` (
`genre_pk` tinyint(2) unsigned NOT NULL AUTO_INCREMENT,
 `genre_name` varchar(90) NOT NULL,
  PRIMARY KEY (`genre_pk`),
 UNIQUE KEY `genre_name` (`genre_name`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=latin1;

LOAD DATA

LOAD DATA INFILE '/path/to/Music_All_2.csv'
INTO TABLE genres FIELDS TERMINATED BY ',' ESCAPED BY '\\'
LINES TERMINATED BY '\n' IGNORE 1 LINES (genre_name);

Reply to OP's comment

1) How to reset AUTO_INCREMENTed value

  • When table has data which shouldn't be deleted.

    ALTER TABLE table_name AUTO_INCREMENT = n;
    
  • When existing data could be deleted. (this is what you've already tried)

    TRUNCATE TABLE table_name;
    

2) '\n' v.s '\r'

That's weird. Generally speaking,

  • '\n' for Unix system
  • '\r\n' for Windows system

I never heard about '\r' for carriage return. MySQL manaul(http://dev.mysql.com/doc/refman/5.5/en/load-data.html) states that

If you have generated the text file on a Windows system, you might have to use LINES TERMINATED BY '\r\n' to read the file properly, because Windows programs typically use two characters as a line terminator. Some programs, such as WordPad, might use \r as a line terminator when writing files. To read such files, use LINES TERMINATED BY '\r'.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top