Peter
Peter

Reputation: 21

Google app engine: filter by ID

I am lost somehow, I want to do something like below which filter by the ID.

id = 1000
query = Customers.all()
query.filter('ID =', id)

or

query = db.GqlQuery("select * from Customers where ID = %s" % id)

What is the correct method to filter by ID?

Upvotes: 2

Views: 954

Answers (3)

fuubah
fuubah

Reputation: 11

you need to use Customers.get_by_id(id)

Upvotes: 1

Olie
Olie

Reputation: 24675

I had this same problem, and it turned out that I was just working too hard. The answer lies in getObjectById(). If this works for you, please go to my very-similar S.O. question and give Gordon's answer a vote-up, since he's the one who showed me this.

Player result = null;
if (playerKey == null)
{
    log.log(Level.WARNING, "Tried to find player with null key.");
}
else
{
    PersistenceManager pm = assassin.PMF.get().getPersistenceManager();

    try {
        result = (Player) pm.getObjectById(Player.class, playerKey);
    } catch (javax.jdo.JDOObjectNotFoundException notFound) {
        // Player not found; we will return null.
        result = null;
    }

    pm.close();
}

return result;

Upvotes: 0

ggarber
ggarber

Reputation: 8360

both are correct and even Customers.gql("WHERE ID = :1", id);

Edit: If ID is the automatically created id property you should use Customers.get_by_id()

Upvotes: 4

Related Questions