-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmysql import
55 lines (42 loc) · 1.43 KB
/
mysql import
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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