Search notes:

Perl module DBI: selectcol_arrayref

By default, the method selectcol_arrayref returns a ref to a list containing the elements of the first column of the result set of the select 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(col1, col2, col3)');

my $sth = $dbh -> prepare('insert into tab values (?, ?, ?)');
$sth -> execute('foo', 'one'  , 'abc');
$sth -> execute('bar', 'two'  , 'def');
$sth -> execute('baz', 'three', 'ghi');

for (@{$dbh -> selectcol_arrayref('select col1 from tab')}) {
  print "$_\n";
}
#
# foo
# bar
# baz
Github repository PerlModules, path: /DBI/selectcol_arrayref.pl
Multiple columns can be selected, however, the result set is not row or column based at all:
#!/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(col1, col2, col3)');

my $sth = $dbh -> prepare('insert into tab values (?, ?, ?)');
$sth -> execute('foo', 'one'  , 'abc');
$sth -> execute('bar', 'two'  , 'def');
$sth -> execute('baz', 'three', 'ghi');

my $colref =  $dbh -> selectcol_arrayref('select * from tab', {Columns=>[1, 2]});

for my $elem (@$colref) {
  print $elem, "\n";
}
# 
# foo
# one
# bar
# two
# baz
# three
Github repository PerlModules, path: /DBI/selectcol_arrayref-multiple-columns.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...', 1759406254, '216.73.216.42', 'Mozilla/5.0 App...', NULL) #2 /home/httpd/vhosts/renenyffenegger.ch/httpsdocs/notes/development/languages/Perl/modules/DBI/selectcol_arrayref(95): insert_webrequest() #3 {main} thrown in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php on line 78