From: Rene Veerman on
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.

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

--
---------------------------------
Greetings from Rene7705,

My free open source webcomponents:
  http://code.google.com/u/rene7705/
  http://mediabeez.ws/downloads (and demos)

My music (i'm DJ firesnake)
  http://mediabeez.ws/music

http://www.facebook.com/rene7705
---------------------------------
From: Chris Rebert on
On Fri, Jul 9, 2010 at 9:56 PM, Rene Veerman <rene7705(a)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
From: John Nagle on
On 7/9/2010 11:08 PM, Dennis Lee Bieber wrote:
> On Sat, 10 Jul 2010 06:56:13 +0200, Rene Veerman<rene7705(a)gmail.com>
> declaimed the following in gmane.comp.python.general:
>
>> 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.

This is really a Google AppEngine question. Ask in their support
forums.

It's worth noting that for many database queries, asking how many
hits there are can cost almost as much as actually retrieving them.

John Nagle
From: Brian Quinlan on

On 10 Jul 2010, at 16:21, John Nagle wrote:

> On 7/9/2010 11:08 PM, Dennis Lee Bieber wrote:
>> On Sat, 10 Jul 2010 06:56:13 +0200, Rene Veerman<rene7705(a)gmail.com>
>> declaimed the following in gmane.comp.python.general:
>>
>>> 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.
>
> This is really a Google AppEngine question. Ask in their support
> forums.

http://groups.google.com/group/google-appengine-python would be a good
place to start.

> It's worth noting that for many database queries, asking how many
> hits there are can cost almost as much as actually retrieving them.

That is the case with the App Engine datastore. If your counts contain
more than a few hundred results then you would be better so store them
somewhere.

Cheers,
Brian