我迷路了,我想做下面的事情,按ID过滤。
id = 1000
query = Customers.all()
query.filter('ID =', id)
或
query = db.GqlQuery("select * from Customers where ID = %s" % id)
按ID过滤的正确方法是什么?
答案 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;