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
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.
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.
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'"
For importing csv with a header row using mysqlimport, just add
--ignore-lines=N
(ignores the first N lines of the data file)
This option is described in the page you've linked.
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
You can put it in the following way:
LOAD DATA LOCAL INFILE 'C:/Users/userName/Downloads/tableName.csv'
INTO TABLE tableName
FIELDS TERMINATED BY ','
LINES TERMINATED BY '\n';
Another option is to use the csvsql command from the csvkit library.
Example usage directly on command line:
csvsql --db mysql:///test --tables yourtable --insert yourfile.csv
This can be executed directly on the command line, or built into a python or shell script for automation if you need to do this for a number of files.
csvsql allows you to create database tables on the fly based on the structure of your csv, so it is a lite-code way of getting the first row of your csv to automagically be cast as the MySQL table header.
Full documentation and further examples here: https://csvkit.readthedocs.io/en/1.0.3/scripts/csvsql.html
来源:https://stackoverflow.com/questions/6605765/importing-a-csv-into-mysql-via-command-line