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

Fatal error: Uncaught PDOException: SQLSTATE[HY000]: General error: 8 attempt to write a readonly database in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php:78 Stack trace: #0 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(78): PDOStatement->execute(Array) #1 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(30): insert_webrequest_('/notes/developm...', 1759406210, '216.73.216.42', 'Mozilla/5.0 App...', NULL) #2 /home/httpd/vhosts/renenyffenegger.ch/httpsdocs/notes/development/languages/Perl/modules/DBI/selectrow_array(117): insert_webrequest() #3 {main} thrown in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php on line 78