Charles Farinella wrote:
> I'm sure this is very simple and I am overlooking something.  I want to
> read a list of bad email addresses from a file and remove them from my
> database.  
> 
> If I print $_, the email addresses are correct, if I try to remove them
> from the db I get errors on just the characters before the @.
>

Are you sure those errors aren't coming from the DB?

> Here is what I have:
> 
> ==
> #!/usr/bin/perl -w
> 
> use strict;
> use DBI;
> use DBD::Pg;
> 
> my $infile = $ARGV[0];
> 
> 
> open( INFILE, "<$infile" );
>         while( <INFILE> ) {
>         my $dbh = DBI->connect("dbi:Pg:dbname=*, "*", "*") || die;
> 
>         #s/\@/\\@/;
>         #print $_;
> 
>         my $sth = $dbh->prepare("UPDATE
>                                         table
>                                 SET
>                                         email = ''
>                                 WHERE
>                                         email = $_ ") || die;

The above value needs to be quoted for the DB not Perl. But it would be
much more efficient to use placeholders and move your statement
preparation outside of the loop and then provide the e-mail address to
the execute. This will take care of your quoting problems and be more
efficient.

> 
>         $sth->execute;
> 
>         finish $sth;
>         $dbh->disconnect;
>         }
> 
> close( INFILE );
> ==
> 

Only do things in the loop that must be done in the loop. Reconnecting,
repreparing, and disconnecting from the DB is a lot of unnecessary work.

perldoc DBI


--Untested--
#!/usr/bin/perl -w

use strict;
use DBI;
use DBD::Pg;

my $infile = $ARGV[0];

# no reason to reconnect for every line
my $dbh = DBI->connect("dbi:Pg:dbname=*, "*", "*") || die;

# statement should only be prepared once
# note use of placeholder ? character
my $sth = $dbh->prepare("UPDATE table SET email = '' WHERE email = ? ")
|| die;

open INFILE, "<$infile" or die "Can't open file for reading: $!";
while ( <INFILE> ) {
   $sth->execute( $_ );
}
close INFILE;

$sth->finish;
$dbh->disconnect;


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to