Dear Wise!

I have a perl script designed to insert/update database table from csv file. It is also meant to be quite universal, so it has not be edited every time it would be used to upload data into different databases

The code is invoked by entering parameters from command line, something like:

 ./scriptname.pl -ignore-lines=1 --table=affiliates --user=username --password=password --dbname=test --delete --fields-terminated-by="," file.csv

The problem is that I'm getting following error

Use of uninitialized value in join or string at ./scriptname.pl line 8 +1, <$fh> line 2. DBD::mysql::st execute failed: Column count doesn't match value count +at row 1 at ./scriptname.pl line 83, <$fh> line 2. Failed to write row Column count doesn't match value count at row 1

and cannot really figure out what should be done to get this script to work as it should. I would really appreciate if you would look at the code below and shed some light on me.

#!/usr/bin/perl # # initial comments section # # # # # # # # # # # # # # # # end of initial comments section # use strict; use warnings; use DBI; use Getopt::Long; use Text::CSV; my $delete = ""; my $table = ""; my $dbname = ""; my $user = ""; my $password = ""; my $terminator = ","; my $ignore = 0; my $optenclosed = '"'; my $db; my $prep; my $fh; GetOptions('delete' => \$delete, 'table=s' =>\$table, 'dbname=s' => \$dbname, 'user=s' => \$user, 'password=s' => \$password, 'fields-terminated-by=s' => \$terminator, 'ignore-lines=i' => \$ignore, 'fields-optionally-enclosed-by=s' => \$optenclosed); # check there is the mandatory options if ($table eq '' or $dbname eq '' or $user eq '' or $password eq '' or + $#ARGV == -1) { die("Incorrect options: Need tablename, database, user, password a +nd at least 1 filename\n"); } # open the database connection $db = DBI->connect("DBI:mysql:$dbname", $user, $password) or die "Cann +ot open database\n"; # delete the contents of the table if delete is set if ($delete) { $prep = $db->prepare("DELETE FROM $table") or die "Cannot prepare + database " . $db->errstr() . "\n"; if (!$prep->execute()) { die "Failed to delete rows" . $db->errst +r() . "\n"; } } # create the csv object my $csv = Text::CSV->new({binary => 1, quote_char => $optenclosed, sep_char => $terminator}); # main loop for each of the passed files foreach my $argnum (0..$#ARGV) { if (!open($fh, '<', $ARGV[$argnum])) { print "Warning: Could not open " . $ARGV[$argnum] . " so file +skipped\n"; } else { # if there is an ignore setting, loop around discarding those +lines for(my $i = 0; $i < $ignore; $i++) { my $row = $csv->getline($fh); } # now perform the database write while (my $row = $csv->getline($fh)) { $prep = $db->prepare("INSERT INTO $table VALUES (\"" . joi +n('","', $csv->fields()) . "\")") or die "Cannot prepare database " . $db->errstr() +. "\n"; if (!$prep->execute()) { die "Failed to write row " . $db- +>errstr() . "\n"; } } $csv->eof or $csv->error_diag(); print "Successfully written $dbname into $table using " . $ARG +V[$argnum]. "\n"; } }

any suggestions would help since I am just starting my adventures with perl


In reply to Use of uninitialized value in join or string by domaniqs

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.