-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcsv-mysql
executable file
·121 lines (88 loc) · 2.67 KB
/
csv-mysql
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#!/bin/bash
set +e;
helpText=;
userName=;
password=;
host=;
port=;
database=;
table=;
fileName=;
function importCSV () {
IFS=,
while read column1 column2
do
[[ -n "$column1" && -n "$column2" ]] && echo "INSERT INTO ${table} (id, vin, inspectionId, createdAt, updatedAt) VALUES (UUID(), '$column1', '$column2', UNIX_TIMESTAMP() * 1000, UNIX_TIMESTAMP() * 1000);" | mysql "-u${userName}" "-p${password}" "${database}";
done < "${fileName}";
}
function displayHowToUse() {
helpText=$(cat << EOF
csv-mysql
Version: 1.0.0
Usage:
csv-mysql [OPTIONS]
Options:
<-f | --filename filename>
<-h | --host host>
<-P | --port port>
<-u | --username username>
<-p | --password password>
<-D | --database database>
<-t | --table table>
</? | --help>
Synopsis:
Purpose: This script copies the contents of a CSV in to a MySQL database whose credentials are supplied.
Options:
-f | --filename: This option receives the name of the CSV file whose content you want to transfer to the Database.
-h | --host: This option receives the host upon which the Database server is running.
-P | --port: This option takes the port number upon which the Database server is running.
-u | --username: This option accepts the username of the database server.
-p | --password: This option receives the user\'s password to the database server.
-D | --database: This option accepts the name of the database server.
-t | --table: This option takes the name of the database table into which the CSV content will be transferred.
/? | --help: This option displays this help page.
Author: Daniel Okwufulueze [https://github.com/DOkwufulueze]
Date: 02/05/2018
EOF
);
}
if [[ $# -gt 0 ]]; then
while [[ "$1" != "" ]]; do
case "$1" in
-f | --filename )
shift;
fileName="$1";
;;
-u | --username )
shift;
userName="$1";
;;
-p | --password )
shift;
password="$1";
;;
-h | --host )
shift;
host="$1";
;;
-P | --port )
shift;
port="$1";
;;
-D | --database )
shift;
database="$1";
;;
-t | --table )
shift;
table="$1";
;;
/? | --help )
displayHowToUse;
echo "${helpText}" | less;
exit;
esac
shift;
done
fi
importCSV >> "./log" 2>&1;