import csv to mysql

#!/bin/bash
db_name=scotchbox
db_user=root
db_password=root

for filename in ./*.csv; do
filebasename=$(basename "$filename")
echo "importing $filebasename"
mysql -u"$db_user" -p"$db_password" -e "
use $db_name; DELETE FROM ${filebasename%.*}; LOAD DATA LOCAL INFILE \"$filebasename\" INTO TABLE ${filebasename%.*} CHARACTER SET 'utf8' FIELDS TERMINATED BY ',' ENCLOSED BY '\"' ESCAPED BY '\"' LINES TERMINATED BY '\n' IGNORE 1 LINES;
"
done

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s