Skip to content

Commit

Permalink
Create mysql import
Browse files Browse the repository at this point in the history
  • Loading branch information
Kaskere authored Oct 6, 2021
1 parent d130df3 commit e53bcfd
Showing 1 changed file with 55 additions and 0 deletions.
55 changes: 55 additions & 0 deletions mysql import
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

0 comments on commit e53bcfd

Please sign in to comment.