Hi

This example is really inefficient and broken.  (I didn't originally post 
any code because the OP obviously hadn't done any reading) 

 
>    test = Test.gql("WHERE name = :1", "blah")   # First check if name=blah 
> exists
>     if test.count(1) == 1: # If Already exists update pagehit += 1
>           test.pagehit = 1 
>           test.put()
>     else:     # If not exists then create new
>           test = Test()
>           test.name = "blah"
>           test.pagehit = 1
>           test.put()
>  
>
>
For starters this code assumes the "name" property value is unique however 
using a property like this does not enforce it in any way, so if more than 
one entity exists with the same name, you will end up updating the first 
entity found with name = "blah". In addition using a query and count is very 
inefficient. Using a key to fetch the entity is more efficient 
and will guarantee uniqueness.  also the value of "test" after the gql query 
in the example code is a query object not an entity.  So the first count(1) 
call will work, however the single entity is not fetched, so the rest of the 
code won't work.


On the basis that the name is unique, the code should look more like the 
following.

test = Test.get_by_key_name("blah")

if test:

    test.pagehit = test.pagehit + 1

else:

    test = Test(key_name="blah")
    test.pagehit = 1

test.put()

Rgds

Tim
    

-- 
You received this message because you are subscribed to the Google Groups 
"Google App Engine" 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/google-appengine?hl=en.

Reply via email to