Not sure why I am getting this error, but I have isolated it to the 2nd prepare statement. If I remove it, the code runs fine. I'm still a newb, so please keep the technical jargon at an elementary level. The program is supposed to open one table on the database, pull all the customerIDs into an array, then the grep function pulls the email, and last the program opens a connection to a new table, and is supposed to upload the id and email. Here is the code with the problem, when it trys to insert the id & email:

#QUERY PREPARATION
my $query = sprintf("INSERT INTO email VALUES (%d, %s)", $servproID, $dbh->quote("$address"));
$dbh->do($query);

Here is the FULL CODE:

use Regexp::Common qw(Email::Address);
use Email::Address;
use DBI;

my $dsn = 'dbi:mysql:servpro:localhost:3306';

my $db = 'servpro';
my $host = 'host';
my $user = 'user';
my $password = 'pass';
my $address = ($addresses[0]);
# DATABASE HANDLE
my $dbh = DBI->connect("DBI:mysql:database=$db;host=$host", $user, $password, {RaiseError => 1});

#QUERY PREPARATION
my $sth = $dbh->prepare("select servproID from fran");
$sth->execute;

while(@row = $sth->fetchrow_array()) {
foreach $row(@row) {
my $filename = "C:/Test/email/$row.txt";

open my $rh, '<', $filename or die "$filename: $!";

my @addresses =
map { m/mailto:($RE{Email}{Address})/o; $1 }
grep { m/href=.+?mailto:/ }
<$rh>
;

close $rh;

{
local $, = local $\ = "\n";
chop(@addresses);
print "$row : @addresses";

#QUERY PREPARATION
my $query = sprintf("INSERT INTO email VALUES (%d, %s)", $servproID, $dbh->quote("$address"));
$dbh->do($query);

}
}
}


In reply to Database Error? DBD:MySQL:DB DO failed: Duplicate entry '0' for key '1' at by jdlev

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.