(MySQL) Load data infile specific columns - mysql

I have a problem loading data into specific columns of an table.
The CSV file is build dynamic with the default fields ID, LAST_REFRESH, ALIAS1 and may contain ALIAS2 to ALIAS8. Current CSV only contains ALIAS1-4
The MySQL table contains the columns ID, LAST_REFRESH, ALIAS1-ALIAS8.
My code for the first file already fails. Code after variables are set is:
LOAD DATA LOCAL INFILE 'C:\\temp\\\OSS001'
INTO TABLE REJECTS (ID, REFRESH_DATE, ALIAS1, ALIAS2, ALIAS3, ALIAS4)
FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\n'
IGNORE 1 LINES
But unfortunately i still receive the following error:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\n'
Anybody knows what i'm doing wrong?

The column names have to be specified last. Read more about it here.
LOAD DATA LOCAL INFILE 'C:\\temp\\\OSS001'
INTO TABLE REJECTS
FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\n'
IGNORE 1 LINES
(ID, REFRESH_DATE, ALIAS1, ALIAS2, ALIAS3, ALIAS4, ALIAS5, ALIAS6, ALIAS7, ALIAS8)

Remove the columns:
LOAD DATA LOCAL INFILE 'C:\\temp\\\OSS001'
INTO TABLE REJECTS FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\n'
IGNORE 1 LINES

Related

Is there a way to ignore header line when importing csv file for mysql? [duplicate]

I'm trying to Load a CSV file into my MySQL database,
But I would like to skip the first line.
I fact It contains the name of my columns and no interesting data.
Here is the query I'm using:
LOAD DATA LOCAL INFILE '/myfile.csv'
INTO TABLE tableName
FIELDS TERMINATED BY ','
ENCLOSED BY '\"'
LINES TERMINATED BY '\n'
(column,column,column);
LOAD DATA INFILE '/tmp/test.txt' INTO TABLE test IGNORE 1 LINES;
(reference)
For those curious, IGNORE N LINES should be after the separator qualifiers:
LOAD DATA LOCAL INFILE '/myfile.csv'
INTO TABLE tableName
FIELDS TERMINATED BY ','
ENCLOSED BY '\"'
LINES TERMINATED BY '\n'
IGNORE 1 LINES
(column,column,column);
Try this:
IGNORE N LINES
LOAD DATA INFILE "/path/to/file.csv"
INTO TABLE MYTABLE
COLUMNS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\n'
IGNORE 1 LINES;

Not retriving the data from csv file to mysql

I wanted to import the csv file into the mysql. I am using this following code:
LOAD DATA local INFILE 'D:\20507.csv'
INTO TABLE rohit1
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n'
IGNORE 1 ROWS
But it is not working; it shows only first column record?
Help me to out.
Try this :
LOAD DATA local INFILE 'D:/20507.csv' INTO TABLE rohit1 FIELDS TERMINATED
BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\r\n' IGNORE 1 LINES;

MySQL LOAD DATA IN FILE Not Inserting all the values

I have a text file in the following format, which contains about a million coupon codes.
93C9LF,PDF934,24YWJ4
93C9LF,PDF934,24YWJ4
93C9LF,PDF934,24YWJ4
Below is my query
LOAD DATA LOCAL INFILE /var/www/coupons.txt
INTO TABLE coupons
FIELDS TERMINATED BY '' ENCLOSED BY '' ESCAPED BY ''
LINES STARTING BY '' TERMINATED BY ','
(coupon);
When I execute this query only some are inserted into the database. I would like to know what I am doing wrong or what could be done to improve this query.
Hi Rezaq you can use this:
LOAD DATA LOCAL INFILE '/var/www/coupons.txt'
INTO TABLE coupons
FIELDS TERMINATED BY '\t'
ENCLOSED BY ','
LINES TERMINATED BY '\n'
(coupon);

how to load a single-column csv file into a specific column in mysql

I have a table with five columns (id,name,age,number,email) where (column 'id' is Pk and has auto_increment) and a single-column csv file containing information only for column 'number'. How can I load this file in mysql.
I have tried this but didn't work:
load data local infile 'C:/Users/user/Downloads/dnd_220116.csv'
into table testing FIELDS TERMINATED BY ','
ENCLOSED BY '"' LINES TERMINATED BY '\r\n' (#dummy,#dummy,number,#dummy);
LOAD DATA LOCAL
INFILE 'C:\\Users\\<user_name>\\Downloads\\dnd_220116.csv'
INTO TABLE `<database_name>`.`testing` CHARACTER SET 'utf8'
FIELDS ESCAPED BY '\\'
TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\r\n'
(`number`);

Insert into SQL from CSV

I have the following CSV File:
AFRICA,Zimbabwe,7,Telecel Zimbabwe,1,0,1,0,0,1
AFRICA,Zambia,7,Celtel Zambia Plc,1,0,1,0,0,1
I am using the following query but for some reason it's giving an error:
LOAD DATA LOCAL INFILE 'pathtofile' INTO TABLE databasename.tablename FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\n';
It is giving a data truncated warning and not importing anything
First of all you need to ensure that the receiving data fields are in the correct order and are of the correct type and size.
Then you specify ENCLOSED BY '"', but your example data is not actually enclosed...
Check The Query in database :
LOAD DATA LOCAL INFILE 'abc.csv' INTO TABLE abc
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\r\n'
IGNORE 1 LINES
(col1, col2, col3, col4, col5...)