#!/usr/bin/perl
use DBI;
my $dbfile = 'test.db'; # your database file
my $need2create = ! -f $dbfile;
my $dbh = DBI->connect( # connect to your database, create if
+ needed
"dbi:SQLite:dbname=$dbfile", # DSN: dbi, driver, database file
"sq", # no user
"lite", # no password
{ RaiseError => 1 }, # complain if something goes wrong
) or die $DBI::errstr;
my $table = 'test';
my @rows = qw(id hobby email);
if($need2create){
print "Creating $dbfile -> $table \n";
$dbh->do("create table $table (".join(',',@rows).')') or die $DBI
+::errstr;
my @DATA = ('1|chess|camelot@aorta.com','2|math|dude@oops.no','3|m
+ath|foo@bar.com');
for my $data (@DATA){
$str ="insert into '$table' ('".join("','",@rows)."')"." value
+s ('".join("','",split(/\|/,$data))."')";
print $str ."\n";
$dbh->do($str) or die $DBI::errstr;
}
}
$sql ="select * from $table";
my $sth = $dbh->prepare($sql);
$sth->execute();
my @SENDTO;
while (my @row = $sth->fetchrow_array) { # retrieve one row
if($row[1]=~/math/i){
print "emailing $row[2]\n";
push(@SENDTO, $row[2]);
# print "". join(", ", @row), "\n";
}
}
for my $who (@SENDTO){
$log = "I have emailed $who\n";
print $log;
&log_it;
}
Prints:
emailing dude@oops.no
emailing foo@bar.com
I have emailed dude@oops.no
I have emailed foo@bar.com
Alternative is that you concatenate your string with newlines, thus:
my $SENDTO = ""; # start with empty string
while (my @row = $sth->fetchrow_array) { # retrieve one row
if($row[1]=~/math/i){
$SENDTO .= "emailing $row[2]\n";
push(@SENDTO, $row[2]);
}
}
$log = $SENDTO;
&log_it;
|