Created
January 10, 2015 23:00
-
-
Save chrisbolin/8e6d68d7f33415eca533 to your computer and use it in GitHub Desktop.
Create MySQL Table from CSV File
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## 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