-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpersons.pl
55 lines (39 loc) · 1.17 KB
/
persons.pl
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
#!/usr/bin/perl -w
# Person ID,Full Name,Email 1
my @inputFiles = ('ClassCalSync_Persons-Students.csv', 'ClassCalSync_Persons-Teachers.csv');
# Database
$DBSVR = 'localhost';
$DBNAME = 'classCalSync';
$DBUSER = '**username**';
$DBPASS = '**password**';
$DBTABLE = 'curPersons';
use HTML::Entities qw(decode_entities);
use utf8;
use Text::CSV;
my $csvIn = Text::CSV_XS->new ( { sep_char => ',' } );
#Prepare use of SQL database
use DBI;
my $dsn = "DBI:mysql:$DBNAME:$DBSVR";
my $dbh = DBI->connect($dsn, $DBUSER, $DBPASS) or die ("Cannot connect");
$dbh->do("TRUNCATE TABLE $DBTABLE");
#Insert data into database
$insert = $dbh->prepare("INSERT into $DBTABLE (ID, Email) values (?, ?)");
foreach $inputFile (@inputFiles)
{
open( my $readRows, '<:encoding(utf-8)', $inputFile)
or die "Could not open $inputFile: $!\n";
while (my $line = <$readRows>)
{
chomp $line;
if ($csvIn->parse($line))
{
my ($personID, $name, $email) = $csvIn->fields();
next if ($email =~ m/Email 1/);
$name = decode_entities($name);
$insert->execute($personID, $email);
}
else { warn "Line could not be parsed: $line\n"; }
}
}
$insert->finish();
exit;