in reply to DBD::Sybase $sth->rows returns -1

Thank you choroba and hotchiwawa. My apology for not checking documentation for DBI. (I was looking around DBD::Sybase)

Would it be possible to use sth->rows or any other method to find the number of rows for select queries? I see that one option is to run each query twice, once the normal query needed and second time by using SELECT COUNT(*) FROM .... by keeping part after "FROM" same and then using $sth->rows on it. I am trying to find the better option.

I tried to call sth->rows after fetching all the rows, i.e. after

while (my @row_18 = $sth18->fetchrow_array) { $role = $row_18[0]; }
but it still returns -1.

How can I call sth->rows after fetching all the rows?

Replies are listed 'Best First'.
Re^2: DBD::Sybase $sth->rows returns -1
by poj (Abbot) on Jan 06, 2016 at 18:30 UTC

    You could use

    my $rowCount; my $role; while (my @row_18 = $sth18->fetchrow_array) { $role = $row_18[0]; ++$rowCount; }

    Are you using the while loop to get the last record of many ?

    poj
      Thank you for your suggestion poj.

      I am using while loop to get all the fetched records. In select queries I am using your suggestion where ever possible or else writing another query with select count(*)

      But even in case of UPDATE queries I am getting -1 for $sth->rows and trying to figure out how to handle that.

        For non-select statements you could use

        my $rowCount = $dbh->do($statement);

        see do

        poj
        Please remove the COMMIT and give a feedback ;)

        Edit
        Please change your execute with this one :)
        $sth18->execute($userName) or die "Couldn't execute statement: " . $st +h18->errstr;
        Execute returns a true value if it succeeds and a false value otherwise, so we abort if for some reason the execution fails.

        Another important thing is that Rows method is database-driver specific, so it might not work in other drivers, or it might work differently in other drivers.