From: Ben Giddings Date: 2003-10-13T02:24:28+09:00 Subject: Re: mysql_num_rows equivalent for DBI? On Sunday, Oct 12, 2003, at 11:08 US/Eastern, Michael Neumann wrote: > Try this one: > > dbh.execute("SELECT Username FROM Users")['pg_row_count'] > > This is a Postgres-specific function, but should return what you > except. Hmm, and there isn't a more general way of doing that, other than returning all entries and doing a .size? > Alternatively you can always use SELECT COUNT(Username). Yeah, but I was hoping to avoid doing two queries. >> irb(main):099:0> sth = dbh.prepare("SELECT Username FROM Users") >> ... >> irb(main):100:0> sth.execute >> [nil] >> irb(main):101:0> sth.rows >> 0 >> irb(main):102:0> sth.fetch_all >> [["ben"], ["harry"]] >> >> This appears to be a bug, doesn't it? > > What exactly should be a bug? That sth.execute returns [nil]? > That's not a bug, but even so should be changed to nil or self. The buggy part is that sth.rows returns 0. From my reading of the DBI documentation (sparse as it is), says that it should return the number of rows processed, or nil. Another poster said that this variable is used only when doing an update or delete. If that's the case, it seems like a SELECT statement should set it to nil. I still think that it should be set to a real value, however. As for sth.execute, I really don't know what that should return, but an array containing just nil doesn't seem very useful. Ben