how do you do a count of a result set?

Chris Rebert clp2 at rebertia.com
Sat Jul 10 01:46:17 EDT 2010


On Fri, Jul 9, 2010 at 9:56 PM, Rene Veerman <rene7705 at gmail.com> wrote:
> hi.
>
> i'm using this function;
>
>
>        def dbCacheGet(self, appParams):
>                results = db.GqlQuery(
>                        "SELECT * "
>            "FROM DBcache "
>            "WHERE url='"+appParams['urlCalled']+"'"
>                ).fetch(1)
>                if results.count('*')==0:
>                        return None
>                else:
>                        return results
>
>
> i dont think this will work correctly.
> i need a function that returns the complete count in the results variable.

But you're returning either None or `results`, neither of which is a
number ("count")...
Also, .count() appears to take an integer, yet you're passing it the string "*".
I thus have little idea what your code is intended to do.

> i searched the docs, but couldn't find it. hope you can help me out here.

(A) **Include the context** for your question next time, i.e. which
library/framework/platform(s) you're using! You're fortunate I just so
happened to recognize the mention of GQL and was able to guess that
you're using Google App Engine.

(B) Define "complete count". Its meaning in this situation is not
obviously clear.


I will *guess* that "complete count" means the total number of query
results, and answer based on that assumption.

I'm not that familiar with GAE, but it /appears/ from the docs that
your only option is to try .count(), and if you hit the max of 1000+
results, either:
* Call .fetch() with higher and higher limits until the results list
stops growing, then return its len(); or
* Iterate over the query object and count the number of results manually.

Here's an attempt at implementing the latter approach:

COUNT_MAX = 1000
def dbCacheGet(self, appParams):
    query_str = "SELECT * FROM DBcache WHERE url='"+appParams['urlCalled']+"'"
    query = db.GqlQuery(query_str)
    count = query.count()
    if count < COUNT_MAX:
        return count
    else:
        return sum(1 for result in query)

Overall, I would guess it's probably more advisable and efficient to
(if possible) just track these counts manually, ahead of time in your
database as you add/remove records; GQL != SQL, so some things must be
done differently. Hence probably why the "count the number of query
results" approach isn't elegant.

Cheers,
Chris
--
http://blog.rebertia.com



More information about the Python-list mailing list