Currently, We have a table similar to this:
---------------------
ID | AField | BField|
---------------------
The ID is Auto Increment
LINES TERMINATED BY '\r\n'
That was the key to success for me when using a text file
If you have only a small number of columns, you can just name them:
LOAD DATA LOCAL INFILE 'myfile.txt' INTO TABLE mytable (AField);
(assuming only ID & AField)
Try to use NULL as the value in the csv file for the ID field.
LOAD DATA INFILE '/tmp/data.csv'
INTO TABLE your_table
FIELDS TERMINATED BY ',' LINES TERMINATED BY '\r'
(AField, BField);
The best thing to do is just include the 2 non-auto-increment columns in the CSV, and then explicitly set the ID column to NULL in the load data infile statement.
Something like this:
LOAD DATA INFILE '/tmp/data.csv'
INTO TABLE your_table
FIELDS TERMINATED BY ','
(AField, BField)
SET ID = NULL;
NULL
in CSV file is read as a STRING in MySQL. If you want to have null values in a MySQL column instead use \N
in place of `NULL. This will fix the issue and you will get the required result.