Google App Engine: Fun with the Datastore

May 12, 2008

The Datastore is the App Engine online database for use by your online application. So not only does Google host your python code but as you accumulate application data and format it as a database they will also manage that data. The last post had adding a form to collect user seed values for the online randomish number generator. Now it’s time to store those seed values in the 1boldlentil application Datastore. Start with adding this line to import the app engine database functionality:

from google.appengine.ext import db

After this a database of seed values should be declared before the main page. This database is pretty simple and at this point consists of the seed value as an integer and the date for that entry. Google has their worked example in their documentation but you have to dig to get a list of types and properties which is here.

class SeedValues(db.Model):
  theSeed = db.IntegerProperty()
  date = db.DateTimeProperty(auto_now_add=True)

Finally we’ll use the two page structure of a simple form to collect a seed value followed by a page to print the values generated with that seed value. So for version five of the application we’ll have a /5s to hold the form and /5 to show the results. The /5s is identical to the /4s from the previous post so it won’t be shown here. The results created with /5 are different though. The top part of this class declaration has theSeed declared and assigns it to the seed value from the form. This value is then put into the database with thisSeed.put().

class version5Page(webapp.RequestHandler):
  def post(self):
    userSeed = int(cgi.escape(self.request.get('theSeed')))
    thisSeed = SeedValues()
    thisSeed.theSeed = userSeed
    self.response.headers['Content-Type'] = 'text/html'
    self.response.out.write('<a href= ')
    self.response.out.write('<b>Randomish Numbers</b></a><p>')
    stop = 25
    i = 1
    while i <= stop:
      i = i + 1
    self.response.out.write('<p><b>Last 10 seed values:</b> ')
    LastTenSeeds = db.GqlQuery("SELECT * FROM SeedValues ORDER BY date DESC LIMIT 10")
    for aSeed in LastTenSeeds:
      self.response.out.write(' ')

After the now too familiar for loop to print out the resulting randomish numbers is a Google Query Language or GQL query for the last ten seed values which are then printed to the screen. Once again pretty simple. Define the specific database class to be used by the application, create an instance and fill it and then access with queries.

The result is: here.

Note that the types and properties also has built in support for GPS coordinates, IM and ratings. No the next post will not be about letting you readers rate each other randomish number generator seed values. But it’s tempting.


4 Responses to “Google App Engine: Fun with the Datastore”

  1. BleedSnoot Says:

    come on…no type checking…?

    Traceback (most recent call last):
    File “/base/python_lib/versions/1/google/appengine/ext/webapp/”, line 501, in __call__*groups)
    File “/base/data/home/apps/1boldlentil/5.1/”, line 128, in post
    userSeed = int(cgi.escape(self.request.get(‘theSeed’)))
    ValueError: invalid literal for int() with base 10: ‘mjhb’

  2. boldlentil Says:

    Fine – yes – no type checking. I was waiting like all weekend to play with the Datastore and if people want to use ‘mjhb’ as a seed value to an esoteric app engine app then yeesh there will be some traceback action. At least I still have the dubugging on so you can out me… although even though I was lazy I thought this would have at least cast the string as an int but I guess not…

  3. pfmagic Says:


    When are you going to blog about something interesting? Feral pumpkins, donuts, stencils, etc. are interesting – Google app engine is really, really boring.


  4. boldlentil Says:

    Huh – you want me to do a feral donut stencil? You’re falling asleep with all this python code? You want me to stick to being interesting all the time? I mean there is a master plan of sorts that has to do with eventually getting to interesting online applications… it just takes some time on the side to get to understanding all the pieces. But OK look it’s probably going to be at least another 3 or 4 app engine posts before I get it out of my system and the wacky app is released… until then I’ll tell you what – I’ll try to get some bonus posts in that are more ‘interesting’.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: