I’m currently attempting to load the below csv file into a table. When I run the below command no errors are received. The csv was generated via Excel on a mac and mysql is running on a Debian Linux vm. What am I doing incorrectly?
CREATE TABLE tmp_xxx_bronto_waitlist (
store BIGINT UNSIGNED NOT NULL,
email VARCHAR(255) NOT NULL,
sku VARCHAR(64) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Command:
mysql > LOAD DATA INFILE '/var/www/local.site/play/migrate_data.example.csv' INTO TABLE tmp_xxx_bronto_waitlist FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY "\r\n" (@store,email,sku) SET store = CAST(@store AS UNSIGNED);
mysql> LOAD DATA INFILE '/var/www/local.site/play/migrate_data.example.csv'
INTO TABLE tmp_xxx_bronto_waitlist
FIELDS TERMINATED BY ',' ENCLOSED BY '"'
LINES TERMINATED BY "\r\n"
(@store,@store_id,@email,@customer_id,@sku,@product_id)
SET store = @store,
store_id = (SELECT store_id FROM bed_core_store WHERE store_id = CAST(@store AS UNSIGNED)),
email = @email,
customer_id = (SELECT entity_id FROM bed_customer_entity WHERE email = @email),
sku = @sku,
product_id = (SELECT entity_id FROM bed_catalog_product_entity WHERE sku = @sku);
The problem was on mac excel saves csv files with \r line ending. Changed “\r\n” to just “\r” and the rows were imported properly.
mysql> LOAD DATA INFILE '/var/www/local.site/play/migrate_data.example.csv'
INTO TABLE tmp_xxx_bronto_waitlist
FIELDS TERMINATED BY ',' ENCLOSED BY '"'
LINES TERMINATED BY "\r"
(@store,@store_id,@email,@customer_id,@sku,@product_id)
SET store = @store,
store_id = (SELECT store_id FROM bed_core_store WHERE store_id = CAST(@store AS UNSIGNED)),
email = @email,
customer_id = (SELECT entity_id FROM bed_customer_entity WHERE email = @email),
sku = @sku,
product_id = (SELECT entity_id FROM bed_catalog_product_entity WHERE sku = @sku);
There was another issue preventing the columns from being populated properly which was fixed by only capturing the values of the csv file. Also casting the data in the csv to the proper data type for comparison in the conditions.
mysql> LOAD DATA INFILE '/var/www/local.site/play/migrate_data.example.csv'
INTO TABLE tmp_xxx_bronto_waitlist
FIELDS TERMINATED BY ',' ENCLOSED BY '"'
LINES TERMINATED BY "\r"
(@store,@email,@sku)
SET store = @store,
store_id = (SELECT store_id FROM bed_core_store WHERE store_id = CAST(@store AS UNSIGNED)),
email = @email,
customer_id = (SELECT entity_id FROM bed_customer_entity WHERE email = CAST(@email AS CHAR(255))),
sku = @sku,
product_id = (SELECT entity_id FROM bed_catalog_product_entity WHERE sku = CAST(@sku AS CHAR(64)));