2012-08-12 30 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

相關問題