Search notes:

Perl module DBI: selectrow_array

selectrow_array returns the first record of a result set as a Perl list.
#!/usr/bin/perl
use warnings;
use strict;

use DBI;

unlink 'the.db' if -e 'the.db';
my $dbh = DBI->connect('dbi:SQLite:dbname=the.db') or die "Could not create the.db";

$dbh -> do('create table tab(col)');

my $sth = $dbh -> prepare('insert into tab values (?)');
$sth -> execute('foo');
$sth -> execute('bar');
$sth -> execute('baz');

my @res = $dbh -> selectrow_array('select count(*), min(col), max(col) from tab'); 
printf "%d %s %s\n", @res;
#
# 3 bar foo


# Using bind variables:
#
@res = $dbh -> selectrow_array('select count(*), min(col), max(col) from tab where col like ?', {}, 'b%'); 
printf "%d %s %s\n", @res;
#
# 2 bar baz
Github repository PerlModules, path: /DBI/selectrow_array.pl
Instead of a select statement, the first parameter can also be an already prepared statement:
#!/usr/bin/perl
use warnings;
use strict;

use DBI;

unlink 'the.db' if -e 'the.db';
my $dbh = DBI->connect('dbi:SQLite:dbname=the.db') or die "Could not create the.db";

$dbh -> do('create table tab (col text)');

my $sth = $dbh -> prepare('insert into tab values (?)');
$sth -> execute('one'  );
$sth -> execute('two'  );
$sth -> execute('three');
$sth -> execute('four' );
$sth -> execute('five' );
$sth -> execute('six'  );
$sth -> execute('seven');
$sth -> execute('eight');
$sth -> execute('nine' );
$sth -> execute('ten'  );
# $dbh -> commit;

$sth = $dbh -> prepare('select count(*), min(col) from tab where col > ?');

my @res = $dbh -> selectrow_array($sth, {}, 'f%');
printf "%d %s\n", @res;
#
# 9 five


@res = $dbh -> selectrow_array($sth, {}, 'l%');
printf "%d %s\n", @res;
#
# 7 nine


@res = $dbh -> selectrow_array($sth, {}, 't%');
printf "%d %s\n", @res;
#
# 3 ten
Github repository PerlModules, path: /DBI/selectrow_array-prepared-stmt.pl

See also

Perl Module DBI, select_all* methods
Perl module DBI

Index