how to Export data from csv file into mysql database? - mysql

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

Related

How to write Load data local infile in unix shell

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;

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;

Decode data from MySQL

I have a database A(using MySQL) inside that there is table T. I have exported the table T using
SELECT * FROM T INTO OUTFILE '/root/table.csv FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\r\n';
Fields of table.csv file have data AVi2B8uZGDdZ9omPqEGS which is not readable. I want to know how to decode the data AVi2B8uZGDdZ9omPqEGS

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