2012-08-12 50 views
1

谁能告诉我,为什么下面的代码是在最后一行导致错误使用命令“执行”与错误消息:谷歌应用程序引擎查询执行只需要3个参数

的方法执行(对象,对象,对象)的查询类型是不适用的参数(长,长,日期,日期)

Query q = pm.newQuery(Appointment.class,"AdminID == AID"+ 
    " && EmployeeID == CEID"+ 
    " && Time > STime"+ 
    " && Time < ETime"); 
q.declareImports("import java.util.Date"); 
q.declareParameters("Long AID, Long CEID, Date STime, Date ETime"); 
q.setOrdering("Time"); 
Appointments = (List<Appointment>) q.execute(AdminID, CurrentEmployeeID, Time1, Time2); 

据我可以告诉(该错误消息暗示,执行功能可以最多只需要3次争论,如果是这样,任何人都可以提出建议要实现我想要做的事情吗?我尝试了以下代码,但每次运行时都会收到解析错误!

Query q = pm.newQuery(Appointment.class,"AdminID == "+AdminID+ 
    " && EmployeeID == "+CurrentEmployeeID+ 
    " && Time > "+Time1+ 
    " && Time < "+Time2); 
q.declareImports("import java.util.Date"); 
q.setOrdering("Time"); 
Appointments = (List<Appointment>) q.execute(); 

解析错误我得到的是:

org.datanucleus.store.query.QueryCompilerSyntaxException:表达的部分无法解析:8月13日十一时44分55秒BST 2012 & &时间< Mon Aug 13 11:45:05 BST 2012

回答

3

尝试executeWithArrayexecuteWithMap

HashMap<String, Object> params = new HashMap<String, Object>(); 
params.put("AID", adminId); 
params.put("CEID", currentEmployeeId); 
params.put("STime", startTime); 
params.put("ETime", endTime); 

query.executeWithMap(params); 

注:

  1. 变量应该lowerCamelCase
  2. startTimeendTimeTime1更具描述性的,Time2
  3. “ID” 通常优于 “ID” - What is correct Java naming convention for id?
  4. 你的第二次尝试将不起作用,因为它隐含地调用..." && Time > " + Time1.toString() + ...
+0

啊,谢谢,那么问题的一部分就解决了......所以现在只是寻找另一种方式来做4个参数的查询! – johnvdenley 2012-08-12 12:19:30

+0

请阅读executeWithArray和executeWithMap。你可能想要executeWithMap({“AID”​​:AdminID,“CEID”:CurrentEmployeeID,“STime”:Time1,“ETime”:Time2}); – 2012-08-12 12:27:47

+0

是的,这似乎是工作,感谢:D – johnvdenley 2012-08-12 16:21:32

相关问题