carlo1973 has asked for the wisdom of the Perl Monks concerning the following question:
I'm sorry to bother everyone again regarding this problem I am having. With the help of everyone here, and variouse resources on the net I think I'm on the cusp of solving problem.
Recap: needing to make a script that would compare the contents of a modified host file in csv format (netbios name, ipaddress) to that of a database that contains similar information. If there are entries missing from the database, the program will report those entries with the information provided from the csv file.
With a lot of help on here, I managed to use script that was given to me and modify it for my needs. The script compiles however but does not appear to run. It just seems to go back to the shell prompt after executing with no errors being displayed.
Here is the current code
#!/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 }
Thank you again :)
This will be my last request for help solving this issue. My internship ends tommorow. :)
I hope to learn the Perl language from the start of my books rather than the hunt and peck method I've been doing thus far.
Carlo Special thanks to scorpio17 for his invaluable assistance!
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Comparing csv text file to mysql db in order to look for missing entries within the db.
by scorpio17 (Canon) on May 28, 2009 at 20:12 UTC | |
by carlo1973 (Initiate) on May 29, 2009 at 13:55 UTC | |
|
Re: Comparing csv text file to mysql db in order to look for missing entries within the db.
by toolic (Bishop) on May 28, 2009 at 19:01 UTC | |
|
Re: Comparing csv text file to mysql db in order to look for missing entries within the db.
by bichonfrise74 (Vicar) on May 28, 2009 at 19:27 UTC |