I started this post while the problem was unsolved but I'd like to
post it anyway for people suffering similar woes and also to see if
anybody has any improvements on my own solution.
Solving the (trivial) problem of counting the number of rows matching
a criterion in a relational database:
result = select('foo', what="count(*)" where="foo.bar = %d" % bar)
How do you retrieve the actual count from the returned object?
Intuitively one would hope to access the result as the first column of
the first row but result[0][0] is not valid, and I can't discover an
attribute name with which to access the field.
A poor solution is to return all matching rows and acquire the count
from len(result), but this is klutzy and won't meet scalability
requirements over a large DB:
count = len(select('foo', where="foo.barid = %d" % barid))
I struck a better solution remembering the AS syntax:
count = select('foo', what='count(*) AS count', where="foo.barid = %d"
% barid)[0].count
However, explicitly accessing the first row with [0] makes this
solution still a little short of perfect. Is there yet a better way?
--
Alex Macmillan
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"web.py" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/webpy?hl=en
-~----------~----~----~----~------~----~------~--~---