2013-08-12 89 views
0

我想通過向SQLite數據庫中添加1來更新'整數'字段。因此,它是X = X+1無法更新SQLite數據庫中的整數字段

以下是我的代碼

public synchronized void updateStatistics(List<String> correctWords, 
      List<String> facedWords, int wordListNumber, boolean flag) { 
     // TODO Auto-generated method stub 
     String[] tableLastNames= {"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"}; 

     String tableName = "WordList_"+tableLastNames[wordListNumber]; 


     if(flag) 
     { 
      //First update 'FacedWords' row 

      Toast.makeText(context, "Faced Words Length: "+facedWords.size(), Toast.LENGTH_LONG).show(); 
      Toast.makeText(context, "Correct Words Length: "+correctWords.size(), Toast.LENGTH_LONG).show(); 

      try 
      { 
       for(int i=0;i<facedWords.size();i++) 
       { 
        String query = "update "+ tableName+" set NumberOfTimesEnglishWordShowed=NumberOfTimesEnglishWordShowed+1 where EnglishWord = ' "+(facedWords.get(i)).trim()+"'"; 
        database.execSQL(query); 
       } 

       for(int i=0;i<correctWords.size();i++) 
       { 
        String query = "update "+ tableName+" set NumberOfTimesEnglishWordCorrected=NumberOfTimesEnglishWordCorrected+1 where EnglishWord = ' "+(facedWords.get(i)).trim()+"'"; 
        database.execSQL(query); 
       } 

       //Toast.makeText(context, "Updated Word: "+, duration) 
      } 
      catch(Exception e) 
      { 
       Toast.makeText(context, "Update error", Toast.LENGTH_LONG).show(); 
      } 

     } 



    } 

我使用的SQLite數據庫瀏覽器中看到我的數據庫中的事情,我注意到上面的代碼無關更新!沒有錯誤!如果你需要,下面是所需的部分從表

String createDatabaseQuery = "create table WordList_A(" + 
        "ID integer primary key autoincrement," 
        +"NumberOfTimesEnglishWordShowed integer," 

        +"NumberOfTimesEnglishWordCorrected integer,");"; 

我使用的更新查詢類似於MS SQL更新查詢,所以我不知道這樣是否正在使用SQLite。爲什麼我的代碼沒有更新任何內容?

請幫

+2

是全球領先的空間'」「+ (facedWords.get(i))。trim()+「''是故意的嗎?這似乎是錯的... –

+0

@Heuster:太棒了!就是這個!謝謝!由於您首先發現問題,因此我想將您的答案標記爲答案。請提供答案作爲答案 –

+2

Tnx,但只是將其他答案標記爲已接受。這不是一場比賽:) –

回答

4

我覺得你有額外的 「空間」 的where子句:

where EnglishWord = ' "+(facedWords.get(i)).trim()+"'" 

所以將其替換爲:

where EnglishWord = '"+(facedWords.get(i)).trim()+"'" 
+0

你好,非常感謝你的回答。有用!我已經給了你一個+1 –

+0

你的歡樂和ty,+1,祝你好運 –