-
Notifications
You must be signed in to change notification settings - Fork 0
/
SL-log-year.pl
executable file
·86 lines (62 loc) · 2.08 KB
/
SL-log-year.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
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
#!/usr/bin/perl
## #!/usr/bin/perl -w strict
# write solarlog data into database
# by Wolfgang Rosner
#
# load credentials
require "./db_cred_writer.pli" ;
# debug printing level 0...3
my $debug=0 ;
die "usage: script.pl path years.csv" if (scalar @ARGV != 2);
my ($datapath, $filename) = @ARGV ;
my $infile = sprintf("%s/%s", $datapath , $filename ) ;
debug_print(2, sprintf ("\tprocessing file %s\n", $infile)) ;
#========================================================
use DBD::mysql; # mysql database access
my $driver = "mysql";
use Data::Dumper; # for debug
use POSIX qw(strftime); # time string formatting
debug_print(1, "\n$0 connecting as user <$user> to database <$database> at host <$DBHost>...\n");
my $dsn = "DBI:$driver:$database;$DBHost";
my $dbh = DBI->connect($dsn, $user, $passwd)
# ; ###### debug - not database
|| die ("Could not connect to database: $DBI::errstr\n");
# || sqlerror($dbh, "", "Could not connect: $DBI::errstr\n");
debug_print(1, "\t...connected to database \n\n") ;
open (INPUT, $infile) || die (sprintf "cannot open >%s< \n", $infile) ;
while(<INPUT>) {
chomp;
unless ($_) {
debug_print (3, "skipping empty line\n");
next;
}
next if /^\s*#/; # skip comment lines
my @fields = split ';' ;
debug_print (2, join '#', @fields) ;
debug_print (2, "\n");
unless (scalar @fields == 3 ) {
debug_print (3, "skipping line with wrong field number\n");
next;
}
my ($date, $inv, $psum) = @fields;
my $sql = "REPLACE INTO `years` (`Date`, `Inv`, `Psum`) VALUES ( ";
$sql .= " STR_TO_DATE('" ;
$sql .= $date;
$sql .= "' , '%d.%m.%y') , ";
$sql .= sprintf ("'%d' , ", $inv);
$sql .= sprintf ("'%d'", $psum);
$sql .= " );" ;
debug_print (2, "SQL-Statement: $sql \n");
# execute sql statement
my $affected = $dbh->do($sql);
debug_print (2, "\t$affected Datasets updated\n");
}
close (INPUT);
$dbh->disconnect ;
exit ;
#============================================
# debug_print($level, $content)
sub debug_print {
$level = shift @_;
print STDERR @_ if ( $level <= $debug) ;
}