Quicksilver has asked for the wisdom of the Perl Monks concerning the following question:

I'm trying to write an update script which goes through a list of names in a flat file directory, checks them against and database and inserts the name if it does not exist but moves on if it does exist. The script appears to be doing the necessary looping and then claiming to insert the new names into the MySQL database but when I check the table later, it is empty. I'd be grateful for some advice on how to solve this, if it an issue with the code?
use strict; use warnings; use DBI; use Email::Find; use File::Find; my $db_id; my $dbh = DBI->connect("dsn", "user", "pword", {'RaiseError' => 1, 'Au +toCommit' => 0}) or die "Connection Error: $DBI::errstr\n"; my $sthuser_insert = $dbh->prepare(q{INSERT INTO role(id,name,bw_name, + role) VALUES (?,?,?,?)}) or die "Couldn't prepare statement: " . $db +h->errstr; my $sthuser_check = $dbh->prepare(q{SELECT name FROM role WHERE name += ?}) or die "Couldn't prepare statement: " . $dbh->errstr; my $dir = "c:\\dir"; get_index_id(); find (\&wanted, $dir); $dbh->disconnect(); sub wanted { my @useroutput; return unless ($_ =~ /\.LIST/i); open (IN, "$_") or die "Can't open $_"; @useroutput = <IN>; close(IN); chomp @useroutput; foreach my $person (@useroutput) { my $finder = Email::Find->new(\&email_found); $finder->find(\$person); } } sub get_index_id { my $db = DBI->connect("dsn", "user", "pword") or die "Connection Er +ror: $DBI::errstr\n"; my $sthlast_user; $sthlast_user=$db->prepare("SELECT MAX(id) FROM role"); $sthlast_user->execute(); ($db_id) = $sthlast_user->fetchrow_array(); $sthlast_user->finish(); $db->disconnect(); print $db_id ." : found and closed\n"; } sub create_random { my @chars = ( "A" .. "Z", "a" .. "z", 0 .. 9, qw(! @ $ ^ & *) ); my $user_name = join("", @chars[ map { rand @chars } ( 1 .. 6 ) ]); return $user_name . $db_id++; } sub email_found { my($email, $orig_email) = @_; my $user = "user"; my $founduser = 0; my $caluser = create_random(). "\@localhost.com"; #need to check if user email exists. If not then add $sthuser_check->execute($orig_email); $founduser = $sthuser_check->fetchrow_array(); $sthuser_check->finish(); if (! $founduser){ if ($orig_email !~ /[a-z, A-Z, 0-9]\@jiscmail.ac.uk/) { print "Inserting $orig_email \n"; $sthuser_insert->execute(+$db_id, lc($orig_email), $caluser, +$user); print "Id: $db_id belongs to $orig_email with name $caluser a +nd role is $user\n"; $sthuser_insert->finish(); next; } else { next; } } else { print "Found " . $founduser ."\n"; next; } return $orig_email; }

Replies are listed 'Best First'.
Re: Trying to update a database but entries not being inserted
by ikegami (Patriarch) on Jan 09, 2009 at 15:10 UTC

    AutoCommit => 0 without ->commit. Your transaction is being rolled back.

    By the way, your die calls will never get executed because you have RaiseError => 1.

      Thanks. Thought it was something stupidly simple but not quite that simple.
Re: Trying to update a database but entries not being inserted
by kennethk (Abbot) on Jan 09, 2009 at 15:10 UTC
    You disable AutoCommit and never invoke a commit method. This means when you disconnect, all changes are automatically rolled back. Try calling $dbh->commit(); before $dbh->disconnect();. More info on the interface can be found at DBI.