Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Importing a csv into mysql via command line

I'm trying to import a very large .csv file (~4gb) into mysql. I was considering using phpmyadmin, but then you have a max upload size of 2mb. Someone told me that I have to use the command line.

I was going to use these directions to import it: http://dev.mysql.com/doc/refman/5.0/en/mysqlimport.html#c5680

What would be the command to set the first row in the .csv table as the column names in the mysql table? This option is available through phpmyadmin, so their must be a mysql command line version too, right?. Please help me. Thank you.

-Raj

like image 900
de1337ed Avatar asked Jul 07 '11 04:07

de1337ed


People also ask

Can you import CSV into MySQL?

MySQL Workbench is a graphical user interface provided by MySQL to manage the database, write the SQL queries, etc. It provides a wizard that allows us to export or import the data in a csv or json format.

How do I import a CSV file into MySQL workbench?

Importing CSV file using MySQL WorkbenchOpen table to which the data is loaded. Review the data, click Apply button. MySQL workbench will display a dialog “Apply SQL Script to Database”, click Apply button to insert data into the table.


4 Answers

Try this command

 load data local infile 'file.csv' into table table  fields terminated by ','  enclosed by '"'  lines terminated by '\n'  (column1, column2, column3,...) 

The fields here are the actual table fields that the data needs to sit in. The enclosed by and lines terminated by are optional and can help if you have columns enclosed with double-quotes such as Excel exports, etc.

For further details check the manual.

For setting the first row as the table column names, just ignore the row from being read and add the values in the command.

like image 174
Balanivash Avatar answered Oct 04 '22 06:10

Balanivash


try this:

mysql -uusername -ppassword --local-infile scrapping -e "LOAD DATA LOCAL INFILE 'CSVname.csv'  INTO TABLE table_name  FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n'" 
like image 29
Rahul Arora Avatar answered Oct 04 '22 06:10

Rahul Arora


You can simply import by

mysqlimport --ignore-lines=1 --lines-terminated-by='\n' --fields-terminated-by=',' --fields-enclosed-by='"' --verbose --local -uroot -proot db_name csv_import.csv

Note: Csv File name and Table name should be same

like image 22
Aravinthan K Avatar answered Oct 04 '22 05:10

Aravinthan K


You could do a

mysqlimport --columns='head -n 1 $yourfile' --ignore-lines=1 dbname $yourfile`

That is, if your file is comma separated and is not semi-colon separated. Else you might need to sed through it too.

like image 36
Niels van Adrichem Avatar answered Oct 04 '22 04:10

Niels van Adrichem