我是 Java、Eclipse、Google App Engine 和 Datastore 的新手,正在编写我的第一个应用程序来自学一些新技术,以用于工作和专业发展目的。
我有一个 Java 应用程序,它可以成功地从我根据 key 创建的数据存储区中保存和检索数据。我现在希望能够根据数据存储中的元代码其他信息执行更一般的查询,例如:
select key_column from my datastore where other_column = "data to be searched"
为了实现这一目标,我进行了一些在线阅读并引用了 O'Reilly 的书籍Programming Google App Engine。我从示例代码开始,但失败并显示错误消息:
An error occured while attempting to contact the server.
Please check your network connect and try again
此时我的代码是:
Query q = new Query("Force");
q.addFilter("ForceName", Query.FilterOperator.EQUAL, input);
PreparedQuery pq = datastore.prepare(q);
for (Entity forceresult : pq.asQueryResultIterable()); {
String newinput = (String) result.getProperty("ForceCode");
}
错误发生在最后一个String netinput
行。
数据存储区代码存在并且很好 - 我已成功写入/读取它。
然而,对于该领域的新手来说,上述示例代码的文档是缺乏的,例如:我假设实体“forceresult”仅存在于 for 循环中,并且不能从外部引用 - 这很好我。但是,在String newinput
行中,我编码了result.getProperty
,尽管在我看来它应该是forceresult.getProperty
> 因为这就是我想引用的内容。
然而,当我编码时,Eclipse 告诉我它无法解析 forceresult
,即使上面的行中定义了 Entity
。
result
是一个我忽略了其重要性的保留字吗?
我的代码在其他方面有错误吗?也许我还没有正确理解 asQueryResultIterable
?
我应该如何解释错误消息:
An error occured while attempting to contact the server.
Please check your network connect and try again
我希望有人能够为我指出正确的方向,提供具体的答案或关于我的学习曲线下一步走向的建议。
最佳答案
您的循环中有错误(;
太多了:))。试试这个:
for (Entity forceresult : pq.asQueryResultIterable()) {
// ^ ; was here
String newinput = (String) result.getProperty("ForceCode");
}
因此,您的循环实际上是空的,并且 result.getProperty(...)
在外部调用,而 forceresult
未定义。
关于java - 查询数据存储时出现错误消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6490579/