Google应用引擎:按ID过滤

时间:2010-08-09 09:01:17

标签: python

我迷路了,我想做下面的事情,按ID过滤。

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

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

按ID过滤的正确方法是什么?

3 个答案:

答案 0 :(得分:4)

两者都是正确的甚至Customers.gql(“WHERE ID =:1”,id);

编辑:如果ID是自动创建的id属性,则应使用Customers.get_by_id()

答案 1 :(得分:1)

您需要使用Customers.get_by_id(id)

答案 2 :(得分:0)

我遇到了同样的问题,事实证明我只是在努力工作。答案在于getObjectById()。如果这对您有用,请转到my very-similar S.O. question并给Gordon的答案投票,因为他是向我展示的人。

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;