2012-03-18 24 views
1

我想知道如何在連接到應用程序引擎的Android客戶端執行RPC調用時防止重複的插入。下面是我的代碼和我在後端嘗試的內容,但是當我這樣做時,我得到一個「內部服務器錯誤」。防止在Android連接的appengine中使用jdo的重複表插入

public void createentity(userentity e) { 
    PersistenceManager pm = PMF.get().getPersistenceManager(); 
    //to go through the records and and check for duplicates 
    Query q = pm.newQuery("select from" + userentity.class + "where Country=='" + e.getCCNumber() + "'"); 
    List <userentity> s = (List <userentity>) q.execute(); 
    //if the size is equal to to null means there is no duplicate 
    if (s.size() == 0) { 
     //insert the value 
     try { 
      pm.makePersistent(e); 
     } finally { 
      pm.close(); 
     } 
    } 
} 
+0

@SachinD當編輯後,請確保您修復後整體,包括代碼格式化(http://jsbeautifier.com可以在許多情況下使用),去除問候/簽名和去除有趣的話。 – 2012-03-18 09:22:57

+0

@ SachinaD..ok謝謝 – 2012-03-18 09:24:44

+0

當您回覆其他評論時,請回復正確的名稱;)在這種情況下,請使用'@ Rob'或'@ RobW'。有關詳情,請參閱[Meta:評論回覆如何工作](http://meta.stackexchange.com/questions/43019/how-do-comment-replies-work)。 *我回復了前一位編輯的帖子,教他如何編輯。* – 2012-03-18 09:28:27

回答

0

我看到了一些潛在的問題。首先,您還應該檢查您的列表是否爲空。如果它爲空,則嘗試訪問大小將導致空指針異常。其次,在你的查詢字符串中刪除double equals。你只需要一個等於那裏。其次,在字符串文字的末尾和開頭添加空格。你正在創建一個無效的字符串。例如,「從...中選擇」。最後,您不需要將e.getCCNumber()包裝爲字符串文字。

Query q = pm.newQuery("select from " + userentity.class + " where Country= " + e.getCCNumber());