#!/usr/bin/perl use strict; use warnings; use Text::CSV_XS; use DBI; my $csv_inputfile = "use_me.txt"; # change me my %data; # hash for storing csv data my $csv = Text::CSV_XS->new; open my $fh, '<', $csv_inputfile or die "can't open $csv_inputfile: $! \n"; while (my $line = <$fh>) { $line =~ s/^\s+//; # strip leading whitespace next unless $line; # ignore blank lines if ( $csv->parse($line) ) { my ($nbname,$ip) = $csv->fields(); next unless $nbname; $data{$nbname} = { IP => $ip || '', NBNAME => $nbname || '', }; } else { my $error = $csv->error_diag(); print "Parse error: line $. : $error.\n"; } } close $fh; my $user = undef; my $pass = undef; my $dbh = DBI->connect("DBI:mysql:pstsize;host=localhost",$user,$pass, {RaiseError => 1, AutoCommit => 1},); unless ($dbh) { die "Could not get database handle."; } my $sth = $dbh->prepare( select_data() ); eval { $sth->execute(); }; if ($@) { die "Database error: ", $dbh->errstr; } while ( my ($nbname, $ip) = $sth->fetchrow ) { unless ($ip) { if ($data{$nbname}{'IP'}) { print ".. but CSV file has value: $data{$nbname}{'IP'}{'NBNAME'}."; } print "\n"; } } $sth->finish; $dbh->disconnect; sub select_data { return <<" END_SQL"; SELECT DISTINCT nbname, ipaddr FROM sizehist END_SQL }