what does $dbh->execute return for a select?



According to the DBI docs:

==========
Perform whatever processing is necessary to execute the prepared statement. An undef is returned if an error occurs. A successful execute always returns true regardless of the number of rows affected, even if it's zero (see below). It is always important to check the return status of execute (and most other DBI methods) for errors if you're not using "RaiseError".

For a non-SELECT statement, execute returns the number of rows affected, if known. If no rows were affected, then execute returns "0E0", which Perl will treat as 0 but will regard as true. Note that it is not an error for no rows to be affected by a statement. If the number of rows affected is not known, then execute returns -1.

For SELECT statements, execute simply "starts" the query within the database engine. Use one of the fetch methods to retrieve the data after calling execute. The execute method does not return the number of rows that will be returned by the query (because most databases can't tell in advance), it simply returns a true value.

If any arguments are given, then execute will effectively call "bind_param" for each value before executing the statement. Values bound in this way are usually treated as SQL_VARCHAR types unless the driver can determine the correct type (which is rare), or unless bind_param (or bind_param_inout) has already been used to specify the type.

If execute() is called on a statement handle that's still active ($sth->{Active} is true) then it should effectively call finish() to tidy up the previous execution results before starting this new execution.
==========

but I'm doing a select * from table and getting back a true value
which is 1. I thought for a successful select, execute would return
0E0 (which is true) but not 1. I was hoping to use true but not
0E0 to identify a non-select statement that affected rows.

The following code using dbd::mysql illustrates this:

#!/usr/bin/perl
use strict;
use warnings;
use DBI;
use Data::Dumper;

my $dbh = DBI->connect(
'DBI:mysql:mjetest', 'xxx', 'yyy',
) or die "DBI::errstr";

$dbh->do(q/drop table if exists mytest/);
$dbh->do(q/create table mytest (a int primary key, b char(20))/);
my $sth = $dbh->prepare(q/insert into mytest values (?,?)/);
$sth->execute(1, 'one');
$sth->execute(2, 'two');
$sth->execute(3, 'three');
$sth = $dbh->prepare("select * from mytest where b = ?");
my $sts = $sth->execute('one');
print "sts = $sts\n";
$ref = $sth->fetchrow_arrayref;

which produces:

sts = 1

but I expected 0E0.

This was with DBI 1.50.

Any ideas? Am I missing something?

Martin
.



Relevant Pages

  • Re: DBI v2 - The Plan and How You Can Help
    ... I can see room for all of them, but all of this is really a DBD, not a DBI, issue. ... and some of them have to resort to search-n-replace in the SQL string at execute() time since the database doesn't have native support. ... ie, on Pg the STHs would be built before the DB is connected, and on Oracle they are built the first time they are used. ... well I am thinking of large program environments here where data dictionaries and generated SQL are the norm. ...
    (perl.dbi.users)
  • request for clarification, was (DBD::DB2 execute and finish problem)
    ... DBI documentation says execute on a an active statement should imply a finish ...
    (perl.dbi.users)
  • Re: Memory access problem with DBI or DBD-Mysql?
    ... It seems that the problems are only with the "do" method and not with the "execute", so I looked for the differences between them. ... I want to repeat that the problem manifest itself only under OpenBSD because of it's memory management that cause the program to segfault if try to access a non allocated memory. ... I'm using DBI 1.45 and DBD-Mysql 2.9008. ...
    (perl.dbi.users)
  • Re: DBI v2 - The Plan and How You Can Help
    ... Darren Duncan wrote: ... of them, but all of this is really a DBD, not a DBI, issue. ... > DBD to decide whether to actually do anything with it immediately or not. ... a database) in your BEGIN block, and execute thousands of times after that. ...
    (perl.dbi.users)
  • Re: Tracing SQL queries done via DBI (mod_perl)
    ... There is a tracemethod on DBI, however, it does not properly print ... dbd_st_prepare calling count_params ... <- execute= 1 at dbitest.pl line 10 ... <- DESTROY= undef during global destruction ...
    (comp.lang.perl.misc)