Fetch data with one row and one column from table using Perl DBI
Asked Answered
E

2

5

I am trying to fetch data like (Select 1 from table) which returns data with one row and one column.

I dont want to use $sth->fetchrow_array method to retreive the data in to array. Is there any way to collect the data into scalar variable direclty?

Ectype answered 4/8, 2013 at 15:38 Comment(0)
T
10

fetchrow_array returns a list —it's impossible to return an array— and you can assign that to anything list-like such as a my().

my $sth = $dbh->prepare($stmt);
$sth->execute();
my ($var) = $sth->fetchrow_array()
   and $sth->finish();

Or you could simply use

my ($var) = $dbh->selectrow_array($stmt);
Tennes answered 4/8, 2013 at 15:41 Comment(1)
The documentation for fetchrow_array and selectrow_array imply they'll return either the first or last column when called in scalar context, so while the parens in my ($var) = ... are normally significant, it should be possible to omit them here.Tennes
N
1
my ($value) = @{$dbh−>selectcol_arrayref("select 1 from table")}

or better

my ($value) = $dbh−>selectrow_array($statement);
Names answered 4/8, 2013 at 23:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.