- or download this
sub table_exists {
my $db = shift;
...
print "table not found!\n";
$dbh->do( $create_query );
}
- or download this
my $create_query = qq{
create table employees (
...
my $fieldlist = join ", ", @fields;
my $field_placeholders = join ", ", map {'?'} @fields;
- or download this
my $insert_query = qq{
INSERT INTO employees ( $fieldlist )
...
my $sth= $dbh->prepare( $insert_query );
# $insert_query = "INSERT INTO employees ( id, name, salary )
# VALUES ( ?, ?, ? )";
- or download this
my ($id, $name, $salary) = (5, 'Sal', 4500);
$sth->execute($id, $name, $salary);
- or download this
while ( get_values($id, $name, $salary) {
$sth->execute($id, $name, $salary);
}
- or download this
my @employees_lol = (
[1, 'Fred', 5000],
...
for (@employees_lol) {
$sth->execute(@$_);
}
- or download this
my @employees_loh = (
{id => 3, name => 'Kim', salary => 5600},
...
for (@employees_loh) {
$sth->execute($_->{id}, $_->{name}, $_->{salary});
}
- or download this
for (@employees_loh) {
$sth->execute(@{$_}{@fields})
}
- or download this
my $query1 = qq{select name from employees};
my $sth= $dbh->prepare($query1);
...
while (my ($name) = $sth->fetchrow_array) {
push @names, $name;
}
- or download this
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));
- or download this
#
# either
...
__END__
$names_ref = ['Fred', 'Joshua', 'Kim', 'Dave', 'Sal'];
@names = ('Fred', 'Joshua', 'Kim', 'Dave', 'Sal');
- or download this
my $sth = $dbh->prepare($query2);
$sth->execute();
...
@{$sth->fetchall_arrayref([0,-2])};
__END__
@names = (qw(Fred Joshua Kim Dave Sal));
- or download this
#
# either
...
[ '4', 'Dave', '6000' ],
[ '5', 'Sal', '4500' ]
];
- or download this
my $sth = $dbh->prepare($query2);
$sth->execute();
...
{ 'salary' => '6000', 'id' => '4', 'name' => 'Dave' },
{ 'salary' => '4500', 'id' => '5', 'name' => 'Sal' }
];
- or download this
my $employees_loh = $dbh->selectall_arrayref($query2, {Slice => {}
+});
- or download this
my $sth = $dbh->prepare($query2);
$sth->execute();
...
{ 'salary' => '4500', 'name' => 'Sal' }
];
- or download this
my %employees_h =
map { $_->[1], $_->[2]}
...
'Dave' => '6000' ,
'Sal' => '4500'
);
- or download this
#
# either
...
'4' => [ 'Dave', '6000' ],
'5' => [ 'Sal', '4500' ]
);
- or download this
my $employees_hoh = $dbh->selectall_hashref($query2, 1);
__END__
...
'4' => { 'salary' => '6000', 'id' => '4', 'name' => 'Dave' },
'5' => { 'salary' => '4500', 'id' => '5', 'name' => 'Sal' }
};
- or download this
my $employees_hoh2 = $dbh->selectall_hashref($query2, 3);
__END__
...
'4500' => { 'salary' => '4500', 'id' => '5', 'name' => 'Sal' },
'6000' => { 'salary' => '6000', 'id' => '4', 'name' => 'Dave' }
};
- or download this
my ($id, $name, $salary) ;
...
3 Kim 5600
4 Dave 6000
5 Sal 4500
- or download this
$sth->execute;
...
$sth->bind_columns ( \( $id, $name, $salary ) );
print "$id\t$name\t$salary\n" while $sth->fetchrow_arrayref;
- or download this
my @empl = (undef, undef, undef);
$sth->execute;
...
$sth->bind_columns ( \( @empl ) );
print join( "\t", @empl), $/ while $sth->fetchrow_arrayref;
- or download this
@empl = ( \$id, \$name, \$salary );
$sth->execute;
$sth->bind_columns( @empl );
print "$id\t$name\t$salary\n" while $sth->fetch;
- or download this
my %rec = (
emp_id => undef,
...
"$rec{first_name}\t",
"$rec{monthly_payment}\n"
while $sth->fetchrow_arrayref;
- or download this
my @fields = (qw(emp_id first_name monthly_payment));
...
"$rec{first_name}\t",
"$rec{monthly_payment}\n"
while $sth->fetchrow_arrayref;
- or download this
use Benchmark (qw(cmpthese));
...
fr_hashref 3858/s -- -56% -59%
man_hashref 8681/s 125% -- -8%
fr_arrayref 9416/s 144% 8% --
- or download this
$sth = $dbh->prepare(qq{
SELECT
...
# using a arrayref
# columns => id,id
# 1 1