getting the number of records in a result set from select

11,280 views
Skip to first unread message

jeff

unread,
Mar 12, 2009, 1:20:44 PM3/12/09
to sqlalchemy
hi. this question should be easy. i've searched around though and
haven't found the answer. all i want to do is know the number of
records in a result set i get using an execute statement with a simple
select. so if i do:

s=select([raw_table],and_(raw_table.c.name==m
['name'],raw_table.c.as_of>=i['first_time']))
rec_list=conn.execute(s)

is there a simple way to get back the number of records that exist in
rec_list?

thanks, i'm sure it's simple and i missed something.

Stephen Emslie

unread,
Mar 13, 2009, 10:31:33 AM3/13/09
to sqlal...@googlegroups.com
Well, I would have expected ResultProxy.rowcount to do just that
(return the number of rows in the last executed statement) but I just
get 0 from it. Perhaps someone could explain how to use it correctly.


Stephen Emslie

Michael Bayer

unread,
Mar 13, 2009, 10:38:29 AM3/13/09
to sqlal...@googlegroups.com

database cursors are essentially iterators so a total rowcount, without
fetching all the rows, is not available in a platform-agnostic way.

the usual strategy to find out how many rows of something exist in the DB
is to do SELECT COUNT(*).

jeff

unread,
Mar 13, 2009, 4:42:14 PM3/13/09
to sqlalchemy
thanks i will use select count (*)

i was making a leap that there would be something in pgdb which allows
a function like:

sql_txt = "select * from addresses"
cursor.execute(sql_txt)
rows=cursor.fetchall()
rows_returned = cursor_result.rowcount

where the rowcount property contains the number of rows returned by
the select statement defined and executed.

just wanted to see whether such a property was available in sqlalchemy
using the relational expression interface.


On Mar 13, 9:38 am, "Michael Bayer" <mike...@zzzcomputing.com> wrote:
> database cursors are essentially iterators so a total rowcount, without
> fetching all the rows, is not available in a platform-agnostic way.
>
> the usual strategy to find out how many rows of something exist in the DB
> is to do SELECT COUNT(*).
>
>
>
> Stephen Emslie wrote:
>
> > Well, I would have expected ResultProxy.rowcount to do just that
> > (return the number of rows in the last executed statement) but I just
> > get 0 from it. Perhaps someone could explain how to use it correctly.
>
> > Stephen Emslie
>
> > On Thu, Mar 12, 2009 at 5:20 PM, jeff <jeffre...@gmail.com> wrote:
>
> >> hi. this question should be easy. i've searched around though and
> >> haven't found the answer. all i want to do is know the number of
> >> records in a result set i get using an execute statement with a simple
> >> select. so if i do:
>
> >> s=select([raw_table],and_(raw_table.c.name==m
> >> ['name'],raw_table.c.as_of>=i['first_time']))
> >> rec_list=conn.execute(s)
>
> >> is there a simple way to get back the number of records that exist in
> >> rec_list?
>
> >> thanks, i'm sure it's simple and i missed something.- Hide quoted text -
>
> - Show quoted text -

Mike Conley

unread,
Mar 13, 2009, 10:30:15 PM3/13/09
to sqlal...@googlegroups.com
If you use

    rows = cursor.fetchall()

you have already executed the query and the result is a list of RowProxy's returned by the query. Count then is simply

    count = len(rows)

Otherwise, the count(*) approach is correct.

--
Mike Conley

jeff

unread,
Mar 13, 2009, 11:10:08 PM3/13/09
to sqlalchemy
thank you that got me where i was trying to get.

originally in the first example i was not adding the fetchall(). len()
and rowcount were not yielding anything in that case. then once
fetchall() was added i used len() as suggested and it worked.

thanks.
> > > - Show quoted text -- Hide quoted text -
Reply all
Reply to author
Forward
0 new messages