How to write Load data local infile in unix shell - mysql

I need to write below statement in unix bash script. I am making mysql connection and passing this whole things in double quotes. I am getting problem in enclosed by clause.
LOAD DATA INFILE 'data.csv' INTO TABLE tbl_name
FIELDS TERMINATED BY ',' ENCLOSED BY '"'
LINES TERMINATED BY '\r\n'
IGNORE 1 LINES;
it should be written like
"LOAD DATA INFILE 'data.csv' INTO TABLE tbl_name
FIELDS TERMINATED BY ',' ENCLOSED BY '"'
LINES TERMINATED BY '\r\n'
IGNORE 1 LINES;"

That's a lot of quoting, but doable.
$: sql=$'LOAD DATA INFILE \'data.csv\' INTO TABLE tbl_name FIELDS TERMINATED BY \',\' ENCLOSED BY \'\"\' LINES TERMINATED BY \'\\r\\n\' IGNORE 1 LINES;'
$: echo "$sql"
LOAD DATA INFILE 'data.csv' INTO TABLE tbl_name FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\r\n' IGNORE 1 LINES;
Just be aware that the quotes are now part of the data as it's handed off to mysql, which will hopefully reparse it accordingly. If I pass it unquoted to printf, notice it still has all the intended quoting characters in place.
$: printf "%s\n" $sql
LOAD
DATA
INFILE
'data.csv'
INTO
TABLE
tbl_name
FIELDS
TERMINATED
BY
','
ENCLOSED
BY
'"'
LINES
TERMINATED
BY
'\r\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;

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`);

(MySQL) Load data infile specific columns

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

how to Export data from csv file into mysql database?

how to Export data from csv file into mysql database?
is there any Query or some othr way avial?
I think you mean IMPORT., here's how:
load data local infile 'yourCSVfilepath.csv' into table tableNameHERE
fields terminated by ','
optionally enclosed by '"'
lines terminated by '\r\n'
To Export data to csv from Mysql use this
SELECT *
INTO OUTFILE '/tmp/tablename.csv'
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
ESCAPED BY '\\'
LINES TERMINATED BY '\n'
FROM tablename
To import csv to Mysql use this
LOAD DATA INFILE 'c:/tablename.csv' INTO TABLE tablename