use strict; use warnings; use DBI; use Compress::Zlib; use File::Find; # DBD::ODBC my $dsn = 'DBI:ODBC:Driver={SQL Server}'; my $host = 'host ip, port'; my $database = 'ProjectTest'; my $user = 'user'; my $auth = 'password'; # Connect via DBD::ODBC by specifying the DSN dynamically. my $dbh = DBI->connect("$dsn;Server=$host;Database=$database", $user, $auth, { RaiseError => 1, AutoCommit => 1}) || die "Database connection not made: $DBI::errstr"; # Find files in specified directory my $dir = 'C:\Users\user\Desktop\Timing Logs'; find(\&print_name, $dir); # Read found files into database sub print_name { if(-f){ #Read csv file line by line my $file = $_; my $gz = gzopen($file, "rb") or die "Cannot open $file: $gzerrno\n"; while ($gz->gzreadline($_) > 0) { my $line = $_; chomp($line); my @array = split /,/, $line; #Match log type # if ($file =~ m/jips/){ # my $sql = "INSERT INTO dbo.JIPS VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?);"; # my $sth = $dbh->prepare( $sql ); # $sth->execute(@array); # } if ($file =~ m/ltms/){ my $sql = "INSERT INTO dbo.LTMS VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?);"; my $sth = $dbh->prepare( $sql ); $sth->execute(@array); } } die "Error reading $file: $gzerrno" if $gzerrno != Z_STREAM_END ; $gz->gzclose(); } }