>From the fine manual:
"Generally, you can only rely on a row count after a non-SELECT execute (for
some specific operations like UPDATE and DELETE), or after fetching all the
rows of a SELECT statement.
For SELECT statements, it is generally not possible to know how many rows
will be returned except by fetching them all. Some drivers will return the
number of rows the application has fetched so far, but others may return -1
until all rows have been fetched. So use of the rows method or $DBI::rows
with SELECT statements is not recommended."
You can, however use COUNT(*) in your query and select the result and get
the proper numbers.
Try:
my $number = $dbh->selectrow_array("SELECT COUNT(*) FROM foo WHERE bar =
'baz'");
----- Original Message -----
From: "Joshua Caesar" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Monday, October 07, 2002 1:19 PM
Subject: problem with sth->rows
| I wrote a program using dbi to keep track of all my buddies, contacts,
| phone numbers , etc from different applications.
| (I want to have one central source for all of the contact info that I
| have for people).
|
| I was able to parse the various data files without a problem. I then
| added code to try and update an MS Access database. Before I add a new
| group into the database,
| I went to check to see if it is there. So I do a select, check to see if
| sth->rows is 0, 1, or more. If it is 0, I insert, if it is 1, I skip
| this group, and if it is more I report the error and die. So when I try
| to run my program, it always die-ed. I couldn't understand why (on an
| empty db), so I added a line to print out the sth->rows.
| Instead of getting an integer I am getting
| "DBI::st=HASH(0x1b177f8)->rows".
|
| I am trying to figure out if there is a problem with my code or
| installation of perl. I re-installed perl and dbi, and still get the
| same error. Yet no one I showed my code to could figure out anything
| wrong.
|
| I have Windows XP, I am using perl 5.6.1 from ActiveState, and am trying
| to connect to a MS Access database.
| I am pasting code below that is my db code stripped out from my parser.
|
| Anyone have any ideas?
|
| Thanks,
| Josh Caesar
| [EMAIL PROTECTED]
| ------------------------------------------------------------------------
| -----------------------
| #!/usr/bin/perl
| use warnings;
| use strict;
| use DBI;
|
| my $dbh; # database handle
| my $DSN = 'dbi:ODBC:contacts';
| my $username = "";
| my $password = "";
| my $sth; # statement handle
| my $sql_check; # sql statement
|
| # automatic error checking
| my %db_attr = ( PrintError => 1, RaiseError => 1 );
|
| # connect to the db
| $dbh = DBI->connect( $DSN, $username, $password, \%db_attr )
| or die "Can't connect to database: $DBI::errstr\n";
|
| #check to see if this group exists
| $sql_check = "SELECT id FROM aim_group_type WHERE name = 'Family';";
| $sth = $dbh->prepare($sql_check);
| unless($sth->execute)
| { $sth->finish(); $dbh->disconnect; die "SQL select see if the group
| exists failed. $DBI::errstr\n$!"; }
|
| print "DEBUG sth rows | $sth->rows |\n"; #how many rows were returned?
| if ($sth->rows == 0) {
| print "Got zero rows, it isn't in the db\n";
| $sth->finish();
| }
| elsif($sth->rows == 1) {
| # this group already exists
| print "Got one row, it is in the db\n";
| $sth->finish();
| }
| else {
| #die too MANY rows
| #always gets here and dies
| $sth->finish();
| $dbh->disconnect;
| die "ERROR More than one group (key value) was returned, this
| shouldn't be\n";
| $sth->finish();
| }
|
| $dbh->disconnect;
| exit;
|