sub table_exists {
my $db = shift;
my $table = shift;
my @tables = $db->tables('','','','TABLE');
if (@tables) {
for (@tables) {
next unless $_;
return 1 if $_ eq $table
}
}
else {
eval {
local $db->{PrintError} = 0;
local $db->{RaiseError} = 1;
$db->do(qq{SELECT * FROM $table WHERE 1 = 0 });
};
return 1 unless $@;
}
return 0;
}
if (table_exists( $dbh, "employees") {
print "it's there!\n";
}
else {
print "table not found!\n";
$dbh->do( $create_query );
}
####
my $create_query = qq{
create table employees (
id int not null primary key,
name char(20) not null,
salary int
)
};
$dbh->do($create_query);
my @fields = (qw(id name salary));
my $fieldlist = join ", ", @fields;
my $field_placeholders = join ", ", map {'?'} @fields;
##
##
my $insert_query = qq{
INSERT INTO employees ( $fieldlist )
VALUES ( $field_placeholders )};
my $sth= $dbh->prepare( $insert_query );
# $insert_query = "INSERT INTO employees ( id, name, salary )
# VALUES ( ?, ?, ? )";
##
##
my ($id, $name, $salary) = (5, 'Sal', 4500);
$sth->execute($id, $name, $salary);
##
##
while ( get_values($id, $name, $salary) {
$sth->execute($id, $name, $salary);
}
##
##
my @employees_lol = (
[1, 'Fred', 5000],
[2, 'Joshua', 5000]
);
for (@employees_lol) {
$sth->execute(@$_);
}
##
##
my @employees_loh = (
{id => 3, name => 'Kim', salary => 5600},
{id => 4, name => 'Dave', salary => 6000},
);
for (@employees_loh) {
$sth->execute($_->{id}, $_->{name}, $_->{salary});
}
##
##
for (@employees_loh) {
$sth->execute(@{$_}{@fields})
}
##
##
my $query1 = qq{select name from employees};
my $sth= $dbh->prepare($query1);
$sth->execute;
my @names =();
while (my ($name) = $sth->fetchrow_array) {
push @names, $name;
}
##
##
my $query2 = qq{select id, name, salary from employees};
my @names = map {$_->[1]}
@{$dbh->selectall_arrayref($query2)};
__END__
@names = (qw(Fred Joshua Kim Dave Sal));
##
##
#
# either
#
my $names_ref = $dbh->selectcol_arrayref($query1);
#
# or
#
my @names = @{$dbh->selectcol_arrayref($query1)};
__END__
$names_ref = ['Fred', 'Joshua', 'Kim', 'Dave', 'Sal'];
@names = ('Fred', 'Joshua', 'Kim', 'Dave', 'Sal');
##
##
my $sth = $dbh->prepare($query2);
$sth->execute();
my @names = map {$_->[1]}
@{$sth->fetchall_arrayref([0,-2])};
__END__
@names = (qw(Fred Joshua Kim Dave Sal));
##
##
#
# either
#
my $employees_lol = $dbh->selectall_arrayref($query2);
#
# or
#
my $sth = $dbh->prepare($query2);
$sth->execute();
my $employees_lol = $sth->fetchall_arrayref();
__END__
$employees_lol = [
[ '1', 'Fred', '5000' ],
[ '2', 'Joshua', '5000' ],
[ '3', 'Kim', '5600' ],
[ '4', 'Dave', '6000' ],
[ '5', 'Sal', '4500' ]
];
##
##
my $sth = $dbh->prepare($query2);
$sth->execute();
my $employees_loh = $sth->fetchall_arrayref({});
__END__
$employees_loh = [
{ 'salary' => '5000', 'id' => '1', 'name' => 'Fred' },
{ 'salary' => '5000', 'id' => '2', 'name' => 'Joshua' },
{ 'salary' => '5600', 'id' => '3', 'name' => 'Kim' },
{ 'salary' => '6000', 'id' => '4', 'name' => 'Dave' },
{ 'salary' => '4500', 'id' => '5', 'name' => 'Sal' }
];
##
##
my $employees_loh = $dbh->selectall_arrayref($query2, {Slice => {}});
##
##
my $sth = $dbh->prepare($query2);
$sth->execute();
my $employees_partial_loh =
$sth->fetchall_arrayref({name =>1, salary=>1});
__END__
$employees_partial_loh = [
{ 'salary' => '5000', 'name' => 'Fred' },
{ 'salary' => '5000', 'name' => 'Joshua' },
{ 'salary' => '5600', 'name' => 'Kim' },
{ 'salary' => '6000', 'name' => 'Dave' },
{ 'salary' => '4500', 'name' => 'Sal' }
];
##
##
my %employees_h =
map { $_->[1], $_->[2]}
@{$dbh->selectall_arrayref($query2)};
__END__
%employees_h = (
'Fred' => '5000' ,
'Joshua' => '5000' ,
'Kim' => '5600' ,
'Dave' => '6000' ,
'Sal' => '4500'
);
##
##
#
# either
#
my %employees_h2 =
map { $_->[0], [ $_->[1], $_->[2] ]}
@{$dbh->selectall_arrayref($query2)};
#
# or
#
my %employees_h2 =
map { shift @$_, [ @$_ ]}
@{$dbh->selectall_arrayref($query2)};
__END__
%employees_h2 = (
'1' => [ 'Fred', '5000' ],
'2' => [ 'Joshua', '5000' ],
'3' => [ 'Kim', '5600' ],
'4' => [ 'Dave', '6000' ],
'5' => [ 'Sal', '4500' ]
);
##
##
my $employees_hoh = $dbh->selectall_hashref($query2, 1);
__END__
$employees_hoh = {
'1' => { 'salary' => '5000', 'id' => '1', 'name' => 'Fred' },
'2' => { 'salary' => '5000', 'id' => '2', 'name' => 'Joshua' },
'3' => { 'salary' => '5600', 'id' => '3', 'name' => 'Kim' },
'4' => { 'salary' => '6000', 'id' => '4', 'name' => 'Dave' },
'5' => { 'salary' => '4500', 'id' => '5', 'name' => 'Sal' }
};
##
##
my $employees_hoh2 = $dbh->selectall_hashref($query2, 3);
__END__
$employees_hoh2 = {
'5000' => { 'salary' => '5000', 'id' => '2', 'name' => 'Joshua' },
'5600' => { 'salary' => '5600', 'id' => '3', 'name' => 'Kim' },
'4500' => { 'salary' => '4500', 'id' => '5', 'name' => 'Sal' },
'6000' => { 'salary' => '6000', 'id' => '4', 'name' => 'Dave' }
};
##
##
my ($id, $name, $salary) ;
$sth = $dbh->prepare($query2);
$sth->execute;
$sth->bind_col(1, \$id);
$sth->bind_col(2, \$name);
$sth->bind_col(3, \$salary);
print "$id\t$name\t$salary\n" while $sth->fetchrow_arrayref;
__END__
1 Fred 5000
2 Joshua 5000
3 Kim 5600
4 Dave 6000
5 Sal 4500
##
##
$sth->execute;
#
# either
#
$sth->bind_columns ( \$id, \$name, \$salary );
#
# or
#
$sth->bind_columns ( \( $id, $name, $salary ) );
print "$id\t$name\t$salary\n" while $sth->fetchrow_arrayref;
##
##
my @empl = (undef, undef, undef);
$sth->execute;
#
# either
#
$sth->bind_columns( map {\$_} @empl );
#
# or (perhaps better)
#
$sth->bind_columns ( \( @empl ) );
print join( "\t", @empl), $/ while $sth->fetchrow_arrayref;
##
##
@empl = ( \$id, \$name, \$salary );
$sth->execute;
$sth->bind_columns( @empl );
print "$id\t$name\t$salary\n" while $sth->fetch;
##
##
my %rec = (
emp_id => undef,
first_name => undef,
monthly_payment => undef
);
$sth->execute;
$sth->bind_col(1, \$rec{emp_id});
$sth->bind_col(2, \$rec{first_name});
$sth->bind_col(3, \$rec{monthly_payment});
print "$rec{emp_id}\t",
"$rec{first_name}\t",
"$rec{monthly_payment}\n"
while $sth->fetchrow_arrayref;
##
##
my @fields = (qw(emp_id first_name monthly_payment));
$sth->execute;
my %rec =();
$sth->bind_columns(map {\$rec{$_}} @fields);
print "$rec{emp_id}\t",
"$rec{first_name}\t",
"$rec{monthly_payment}\n"
while $sth->fetchrow_arrayref;
##
##
use Benchmark (qw(cmpthese));
$sth = $dbh->prepare($query2);
cmpthese (50000, { # adjust this to the speed of your machine
fr_hashref => sub {
$sth->execute;
while (my $hash = $sth->fetchrow_hashref) { }
},
fr_arrayref => sub {
$sth->execute;
while (my $aref = $sth->fetchrow_arrayref) { }
},
man_hashref=> sub{
my %hash = (id =>undef, name => undef, salary => undef);
$sth->execute;
$sth->bind_columns( \$hash{id},
\$hash{name}, \$hash{salary});
while ($sth->fetchrow_arrayref) { }
}
});
__END__
(output edited for display purposes)
Benchmark: timing 50000 iterations of
fr_arrayref, fr_hashref, man_hashref...
fr_arrayref: 5 wallclock secs ( 4.68 usr + 0.63 sys = 5.31 CPU)
fr_hashref: 13 wallclock secs (12.13 usr + 0.83 sys = 12.96 CPU)
man_hashref: 6 wallclock secs ( 5.08 usr + 0.68 sys = 5.76 CPU)
Rate fr_hashref man_hashref fr_arrayref
fr_hashref 3858/s -- -56% -59%
man_hashref 8681/s 125% -- -8%
fr_arrayref 9416/s 144% 8% --
##
##
$sth = $dbh->prepare(qq{
SELECT
a.id, b.id
FROM
employees a
INNER JOIN employees b ON (a.id=b.id)
WHERE a.id = 1
});
$sth->execute;
print "using a hashref\n";
while (my $hashref = $sth->fetchrow_hashref) {
for (keys %$hashref) {
print "$_ => $hashref->{$_}\t"
}
print $/;
}
#
# using a hashref
# id => 1
#
$sth->execute;
print "using a arrayref\n";
print "columns => ",join( ",", @{$sth->{NAME}}), $/;
while (my $aref = $sth->fetchrow_arrayref) {
print "@$aref\n";
}
#
# using a arrayref
# columns => id,id
# 1 1