forked from Kaskere/import-csv-to-mysql
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
55 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
#!/bin/bash | ||
|
||
# show commands being executed, per debug | ||
set -x | ||
|
||
# define database connectivity | ||
_db="" | ||
_db_user="" | ||
_db_password="" | ||
|
||
# define directory containing CSV files | ||
_csv_directory=/home/ubuntu | ||
|
||
# go into directory | ||
cd $_csv_directory | ||
echo $_csv_directory | ||
# get a list of CSV files in directory | ||
_csv_files=`ls -1 *.csv` | ||
echo $_csv_files | ||
# loop through csv files | ||
for _csv_file in $_csv_directory/*.csv ; | ||
do | ||
|
||
# remove file extension | ||
_csv_file_extensionless=`echo $_csv_file | sed 's/\(.*\)\..*/\1/'` | ||
|
||
# define table name | ||
_table_name="${_csv_file_extensionless}" | ||
|
||
# get header columns from CSV file | ||
_header_columns=`head -1 $_csv_file | tr ',' '\n' | sed 's/^"//' | sed 's/"$//' | sed 's/ /_/g'` | ||
_header_columns_string=`head -1 $_csv_file | sed 's/ /_/g' | sed 's/"//g'` | ||
|
||
# ensure table exists | ||
mysql -u $_db_user -p$_db_password $_db << eof | ||
CREATE TABLE IF NOT EXISTS \`$_table_name\` ( | ||
id int(11) NOT NULL auto_increment, | ||
PRIMARY KEY (id) | ||
) ENGINE=MyISAM DEFAULT CHARSET=latin1 | ||
eof | ||
|
||
# loop through header columns | ||
for _header in ${_header_columns[@]} | ||
do | ||
|
||
# add column | ||
mysql -u $_db_user -p$_db_password $_db --execute="alter table \`$_table_name\` add column \`$_header\` text" | ||
|
||
done | ||
|
||
# import csv into mysql | ||
mysqlimport --fields-enclosed-by='"' --fields-terminated-by=',' --lines-terminated-by="\n" --columns=$_header_columns_string -u $_db_user -p$_db_password $_db $_csv_directory/$_csv_file | ||
|
||
done | ||
exit |