Skip to content

Instantly share code, notes, and snippets.

@egel
Created April 11, 2016 08:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save egel/e3f22e5ca4279c219c9be3f9442f6229 to your computer and use it in GitHub Desktop.
Save egel/e3f22e5ca4279c219c9be3f9442f6229 to your computer and use it in GitHub Desktop.
Simple script that help you to insert multiple csv data into MySQL database table
#!/bin/bash
read -r -p "Type MySQL user (root): " USER
if [ -z "$USER" ]; then
USER=root
fi
read -r -p "Type MySQL $USER password: " PASS
read -r -p "Type MySQL database name: " DBNAME
read -r -p "Type MySQL database table: " DBTABLE
INPUT=ietf-language-tags.csv
[ ! -f $INPUT ] && { echo "$INPUT file not found"; exit 99; }
# All columns from CSV must be defined, otherwise it concate all other to the last column
while IFS="," read lang langType territory revGenDate defs dftLang file
do
echo "USE $DBNAME; INSERT INTO $DBTABLE (nameNative, nameEnglish, ietfCode, langType, territory) VALUES ('', '', '$lang', '$langType', '$territory');"
done < $INPUT | mysql -u $USER -p$PASS;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment