Is there a way to import multiple csv files at the same time into a MySQL database? Some sort of batch import?
I'm on Mac OSX running a MAMP server.
I have 185 csv files that I need to import into a MySQL table. I can import them individually using phpMyAdmin's import tab, but it would take a long time. Does anyone know if there is a better way?
Use a shell script like this:
#!/usr/bin/env bash
cd yourdirectory
for f in *.csv
do
mysql -e "USE yourDatabase LOAD DATA LOCAL INFILE '"$f"'INTO TABLE yourtable"
done
There's a little PHP script for you:
#!/usr/bin/php
<?
mysql_connect('localhost','root','root'); // MAMP defaults
mysql_select_db('yourdatabase');
$files = glob('*.csv');
foreach($files as $file){
mysql_query("LOAD DATA INFILE '".$file."' INTO TABLE yourtable");
}
See the MySQL Manual for LOAD DATA INFILE options which fit your documents.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With