Skip to content

Instantly share code, notes, and snippets.

@bosukh
Created November 26, 2017 07:16
Show Gist options
  • Save bosukh/7724b544a326f20895570a36d98a2680 to your computer and use it in GitHub Desktop.
Save bosukh/7724b544a326f20895570a36d98a2680 to your computer and use it in GitHub Desktop.
#!/bin/bash
if [ "$#" -ne 5 ]; then
echo "Wrong number of arguments to the import shell script."
exit 1
fi
csv_filename=$1
table_name=$2
db_name=$3
username=$4
password=$5
mysql -u$username -p$password <<QUERY_INPUT
USE $db_name;
DROP TABLE IF EXISTS temp_table;
CREATE TABLE temp_table LIKE $table_name;
ALTER TABLE temp_table DROP COLUMN date_of_birth;
SET foreign_key_checks=0;
SET unique_checks=0;
SET sql_log_bin=0;
SET autocommit=0;
LOAD DATA LOCAL INFILE '$csv_filename' INTO TABLE temp_table
FIELDS TERMINATED BY '^'
LINES TERMINATED BY '\n';
COMMIT;
UPDATE $table_name AS Original
INNER JOIN temp_table AS New USING(id)
SET Original.name = New.name;
COMMIT;
DROP TABLE temp_table;
COMMIT;
SET sql_log_bin=1;
SET unique_checks=1;
SET foreign_key_checks=1;
SET autocommit=1;
QUERY_INPUT
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment