in reply to Re: Use of uninitialized value in join or string
in thread Use of uninitialized value in join or string

Many thanks for explaining how useful placeholders are to me, @roboticus

I have replaced code:

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"; } }

with the code that you have suggested to me, but the script produces another error:

DBD::mysql::st execute failed: Column count doesn't match value count at row 1 at ./test.pl line 85, <$fh> line 2.

and so on for every record of the file to be imported.

which makes no sense to me, as from my understanding when I use map { expr } list, it evaluates expr for every item in list, so it should update the list automatically, isn't that right?

Replies are listed 'Best First'.
Re^3: Use of uninitialized value in join or string
by hippo (Archbishop) on Jan 29, 2014 at 13:20 UTC

    Your code snippet here isn't actually using placeholders, so you may not have quite grasped what they are. See how roboticus is using the question marks in his SQL? See how he calls execute with the field list? These are things which you should be doing.

      Thank you hippo for pointing this out, however, the snippet I've inserted into my recent post, is the piece of code, that was removed and replaced by the code, that roboticus kindly suggested to me in his post.

      So instead of:

      my $SQL = "INSERT INTO $table VALUES (" . join(",", map { "?" } $csv->fields) . ")"; $prep = $db->prepare($SQL) or die "..."; while (my $row = $csv->getline($fh)) { $prep->execute($csv->fields); }

      I used:

      my $SQL = "INSERT INTO $table VALUES (" . join(",", map { "?" } $csv->fields) . ")"; $prep = $db->prepare($SQL) or die "Cannot prepare database"; while (my $row = $csv->getline($fh)) { $prep->execute($csv->fields); }

      It does not change the fact, that I still struggle with this new error:

      DBD::mysql::st execute failed: Column count doesn't match value count at row 1 at ./test.pl line 85, <$fh> line 2.

        Sorry, I misread your post. Your placeholders there look OK at first pass.

        So, for starters I would use @$row as the argument to execute and then I would compare the number of fields in $row with the number of fields in your database for $table and see where the mismatch occurs. HTH.