【问题标题】:Google app engine: filter by IDGoogle 应用引擎:按 ID 过滤
【发布时间】:2012-06-21 04:51:51
【问题描述】:

不知怎的,我迷路了,我想做一些类似下面按 ID 过滤的事情。

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

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

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

【问题讨论】:

    标签: python


    【解决方案1】:

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

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

    【讨论】:

    • 我试过了,但都没有返回结果,所以我想知道 ID 的正确语法是什么?
    • 我是否需要在 index.yaml 中写一些东西来让这个 get_by_id 函数使用默认的 id?
    【解决方案2】:

    你需要使用Customers.get_by_id(id)

    【讨论】:

    • 是否需要设置 index.yaml 才能让这个函数使用默认 id?
    【解决方案3】:

    我也遇到了同样的问题,结果证明我工作太努力了。答案在于 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;
    

    【讨论】:

    • 嘿。糟糕,抱歉——我刚刚注意到你想要 python,而我给了你 Java。好吧,我确定在 python API 中也有一个 getObjectByID 方法。祝你好运!
    猜你喜欢
    • 2023-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-16
    • 1970-01-01
    • 1970-01-01
    • 2010-12-22
    • 2018-11-25
    相关资源
    最近更新 更多