my @results = &row_sql(qq~ select * from table where ...; ~);
my @results = &get_sql(qq~ select id from table where ...; ~);
my $rows = &hash_sql(qq~ select * from table; ~);
foreach my $row (@$rows){
print "$row->{id}, $row->{somecolumn}\n";
}
####
until (
$dbh=DBI->connect_cached($connectionInfo,$user,$passwd, {PrintError=>0} )
) { if ($sqlw){print "Retrying SQL Connect ($DBI::errstr) ...";} sleep(1); }
until (
eval {
$sth=$dbh->prepare($insert);
if ($sth->execute()) {
$sth->finish;
}
}
) { if ($sqlw){print "Retrying SQL Insert ($DBI::errstr)...";} sleep (1); $retry_count++;}
##
##
use DBI;
&sql_setup;
$sqlw = 0; #Don't print SQL warnings
sub sql_setup{
if ($_ eq ""){
$db="YOUR DATABASE";
}else{
$db = $_[0];
}
$user="YOUR USER";
$passwd="YOUR PASSWORD";
$host="YOUR SERVER:3306";
$connectionInfo="dbi:mysql:$db;$host";
$sql = 1;
}
sub del_sql{
if ($_[0] eq ""){print "Empty Delete";}
&sql($_[0]);
}
sub mod_sql{
if ($_[0] eq ""){print "Empty Modify";}
&sql($_[0]);
}
sub put_sql{
if ($_[0] eq ""){print "Empty Put";}
&sql($_[0]);
}
sub row_sql(){
$sql_r++;
my $select = $_[0];
my @row,$dbh,$sth,$retry_count;
if ($select eq ""){print "Empty Select."; exit;}
until (
$dbh=DBI->connect_cached($connectionInfo,$user,$passwd, {PrintError=>0} )
) { if ($sqlw){print "Retrying SQL Connect ($DBI::errstr) ...";} sleep(1); }
until (
eval {
$sth=$dbh->prepare($select);
if ($sth->execute()) {
@row=$sth->fetchrow_array();
$sth->finish;
}
}
) { if ($sqlw){print "Retrying SQL Row ($DBI::errstr)...";} sleep (1); $retry_count++;}
return @row;
}
sub get_sql(){
$sql_g++;
my $select = $_[0];
my $c = 0;
my @results,@row,$dbh,$sth;
if ($select eq ""){print "Empty Select."; exit;}
until (
$dbh=DBI->connect_cached($connectionInfo,$user,$passwd, {PrintError=>0} )
) { if ($sqlw){print "Retrying SQL Connect ($DBI::errstr) ...";} sleep(1); }
until (
eval {
$sth = $dbh->prepare($select);
if ($sth->execute()) {
while (@row=$sth->fetchrow_array()) {
@results[$c] = @row[0];
$c++;
}
$sth->finish;
}
}
) { if ($sqlw){print "Retrying SQL Get ($DBI::errstr)...";} sleep (1); $retry_count++;}
return (@results);
}
sub hash_sql(){
$sql_h++;
my $select = $_[0];
my $dbh,$sth,$retry_count,$rows;
if ($select eq ""){print "Empty Select."; exit;}
until (
$dbh=DBI->connect_cached($connectionInfo,$user,$passwd, {PrintError=>0} )
) { if ($sqlw){print "Retrying SQL Connect ($DBI::errstr) ...";} sleep(1); }
until (
eval {
$rows = $dbh->selectall_arrayref(
$select,
{ Slice => {} }
);
}
) { if ($sqlw){print "Retrying SQL Hash Select ($DBI::errstr)...";} sleep (1); $retry_count++;}
return ($rows);
}
sub sql{
$sql_a++;
my @row,$dbh,$sth,$retry_count;
my $insert = $_[0];
if ($insert eq ""){print "Empty insert."; exit;}
until (
$dbh=DBI->connect_cached($connectionInfo,$user,$passwd, {PrintError=>0} )
) { if ($sqlw){print "Retrying SQL Connect ($DBI::errstr) ...";} sleep(1); }
until (
eval {
$sth=$dbh->prepare($insert);
if ($sth->execute()) {
$sth->finish;
}
}
) { if ($sqlw){print "Retrying SQL Insert ($DBI::errstr)...";} sleep (1); $retry_count++;}
return 1;
}