Skip to content

Instantly share code, notes, and snippets.

@chrisbolin
Created January 10, 2015 23:00
Show Gist options
  • Save chrisbolin/8e6d68d7f33415eca533 to your computer and use it in GitHub Desktop.
Save chrisbolin/8e6d68d7f33415eca533 to your computer and use it in GitHub Desktop.
Create MySQL Table from CSV File
## prints create and insert SQL statements from a csv ##
# csv-to-table [path] [name]
# $ csv-to-table ./data.csv data_table
# create table adapted from John Swapceinski,
# http://dev.mysql.com/doc/refman/5.0/en/load-data.html#c12001
path=$(pwd)
echo "## create table ##"
echo "create table $2 ( "
head -1 $1 | sed -e 's/,/ varchar(255),/g'
echo " varchar(255) );"
echo
echo "## insert data ##"
echo "load data"
echo "local infile '$path/$1'"
echo "into table vars"
echo "fields terminated by ','"
echo "lines terminated by '\n';"
echo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment