Question

Im working with the dataStore trying to perfom a simple query, but it doesnt work as supposed to:

class User_Machine(db.Model):
    machine_id = db.IntegerProperty (required = True)
    username = db.StringProperty (required = True)
    last_call = db.DateTimeProperty (auto_now = True)

def query(username, machine_id)
    q = db.GqlQuery('SELECT * FROM User_Machine WHERE username=:1 AND machine_id=:2', username, machine_id)
    r = q.get()
    return r

print query('uherran', 23)

And my response is:

None

And I was expecting to read this record:

The values in the datastopre viewer are:

Enitty Kind  User_Machine
Entity Key   ahBkZXZ-Z2VzdG9yYXZpc29zchILEgxVc2VyX01hY2hpbmUYKAw
ID   40
username (string)   uherran
last_call (datetime)    2012-08-19 09:57:35
machine_id (long)       23

There must be something completely wrong in my understanding on how querys go.

Anyone can help?

Thanks.

In fact. I adapted a little but my code to provide clarity, but it seems I didnt make a propper error check.

The code is:

def check_machine_username(self, machine_id, username):
        key='CHECK_MACHINE_USERNAME_'+machine_id+'_'+username
        q= memcache.get(key)
        if q:
            return True
        else:
            logging.info(username)
            logging.info(machine_id)
            q = dbstructure.User_Machine.all()
            q.filter('username = ', username)
            q.filter('machine_id = ', machine_id)

            if q.get():
                loggin.info('query right')
                memcache.set(key,True)
                return True
            else:
                logging.info('query wrong')
                return False

No matter what I have in my User_machine db that query always returns empty.

Was it helpful?

Solution

My guess is that in the failing case your machine_id is a string, whereas it should be an int.

OTHER TIPS

The problem as I notice it, was that machine_id was an integer and I was using a string.

q.filter('username = ', username)
q.filter('machine_id = ', int(machine_id))
                          ^^^

Its the bad thing about asuming a type.

Thanks to every one.

It looks like it is because you aren't returning the value of the function back to your print (I may or may not do this all the time as well :) ). Try this:

def query(username, machine_id)
    q = db.GqlQuery('SELECT * FROM User_Machine WHERE username=:1 AND machine_id=:2', username, machine_id)
    r = q.get()
    return r # or just return q.get()

Note that this will return a User_Machine object (and not a formatted dictionary/other response like you may be expecting) which you'll then need to handle (e.g. instead of print, assign it to a result variable and then you can access its attributes by doing result.machine_id, etc.).

You can also use a Query object to accomplish the same thing:

def query(username, machine_id)
    # Same as using SELECT * in the Gql query above)
    q = User_Machine.all()

    # Then apply the filters (you can also do q.filter(..).filter(..))
    q.filter('username = ', username)
    q.filter('machine_id = ', machine_id)
    return q.get()
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top