0

I have a .csv file which I want to load into a table via MySQL. If I have my SQL query as:

CREATE TABLE table_name (
id INT NOT NULL AUTO_INCREMENT,
playerID VARCHAR(40),
class VARCHAR(40),
system VARCHAR(40),
lvl INT,
build VARCHAR(40),
...
PRIMARY KEY (id)
);

Then my table consists of

id playerID class system lvl build ...

1 monk ps4 70 0 77 ...

whereas if I include the id at the end of the CREATE TABLE query:

CREATE TABLE table_name (
playerID VARCHAR(40),
class VARCHAR(40),
system VARCHAR(40),
lvl INT,
build VARCHAR(40),
...
id INT NOT NULL AUTO_INCREMENT,
PRIMARY KEY (id)
);

then the id field correctly auto-increments and the playerID, class, etc. are correctly entered:

playerID class system lvl build ...

Player1 monk ps4 70 0 ...

In either case, the way I am reading in the data is:

LOAD DATA LOCAL INFILE '/path/to/data.csv'
INTO TABLE table_name
FIELDS TERMINATED  BY ','
LINES TERMINATED BY '\n'
IGNORE 1 ROWS;

So I am assuming the problem is that I am reading the data in with id being sent to the first argument in the .csv file, but how do I edit my queries to input an auto-incrementing Primary Key as the first column in the table?

EDIT:

The basic syntax of the .csv file is:

playerID, class, system, lvl, build, ...

player1, monk, ps4, 70, dual, ...

2
  • Format of the CSV would be useful. I think what you are saying is your CSV doesn't have ID's and that you want the DB to generate them?
    – AndySavage
    Nov 12, 2014 at 0:39
  • yes, I will edit and add a basic syntax of lines for the .csv file but it follows the second convention of 'PlayerID, class, system, lvl, build, ... '
    – drjrm3
    Nov 12, 2014 at 0:48

1 Answer 1

1

You can specify a column list to handle cases when the CSV doesn't align with the table schema, for example

LOAD DATA LOCAL INFILE '/path/to/data.csv'
INTO TABLE table_name (playerID, class, system, lvl, build)
FIELDS TERMINATED  BY ','
LINES TERMINATED BY '\n'
IGNORE 1 ROWS;

See http://dev.mysql.com/doc/refman/5.0/en/load-data.html

1
  • If I do this, will it automatically generate a column 1 which is the id?
    – drjrm3
    Nov 12, 2014 at 2:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.