8

I am looking for a way to insert all my csv files into MySQL without having to write several LOAD DATA INFILE statements.

I have many csv files I need to insert and the files themselves are very large.

I have tried *.csv, but this does not work.

Paul White
  • 94,921
  • 30
  • 437
  • 687
dunce1
  • 201
  • 1
  • 2
  • 4

2 Answers2

3

CW answer originally left in the question by its author

I wrote a script which worked:

for f in /var/lib/mysql/[insert name of database here]/*.csv
do
    mysql -e "LOAD DATA INFILE '"$f"' INTO TABLE [nameoftable] 
      FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n' IGNORE 1 LINES" 
      -u [username] --password= [password] [name of database]
echo "Done: '"$f"' at $(date)"
done
Paul White
  • 94,921
  • 30
  • 437
  • 687
3

See these previous answers:

https://stackoverflow.com/questions/8538995/how-to-import-multiple-csv-files-into-a-mysql-database

https://stackoverflow.com/questions/6552042/mysql-loading-multiple-files-into-a-table

Essentially, no. But it's easy to script this to get a similar result (e.g. one command to import multiple files using a loop).

Phil Sumner
  • 1,906
  • 11
  • 13