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;
}
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.