2012-01-20 54 views
0

我創建了一個數據庫,用於存儲來自互聯網的數據。當有互聯網連接時,我調用createEntry並將數據寫入數據庫。我的問題是,第二次使用互聯網,新的數據並沒有覆蓋舊的,但創建新的條目。所以,我認爲解決方案將更新我的數據庫從第二次,用戶有互聯網連接,所以每次都有相同數量的數據。我的問題是,我不知道該怎麼做。這是我第一次嘗試SQLite和數據庫。如何更新我的數據庫

這是我createEntry:

public void createEntry(String title, String getagonistiki, String getskor, 
      String getgipedo, String date, String getgoal1, String getgoal2, 
      String teliko_skor) { 
     // TODO Auto-generated method stub 
     ContentValues cv = new ContentValues(); 
     cv.put(DBHelper.TITLE, title); 
     cv.put(DBHelper.AGONISTIKI, getagonistiki); 
     cv.put(DBHelper.SKOR, getskor); 
     cv.put(DBHelper.GIPEDO, getgipedo); 
     cv.put(DBHelper.DATE, date); 
     cv.put(DBHelper.GOALA, getgoal1); 
     cv.put(DBHelper.GOALB, getgoal2); 
     cv.put(DBHelper.DESCRIPTION, teliko_skor); 

     try { 
      ourDatabase.insert("osfpDB", null, cv); 
     } // ourDatabase.update("osfpDB",cv,DBHelper.ROWID,null); 

     catch (Exception e) { 
      Log.e("DB ERROR ON .INSERT", e.toString()); // prints the error 
                 // message to the log 
      e.printStackTrace(); // prints the stack trace to the log 
     } 
    } 

,這就是IM通話時,有互聯網連接:

HotOrNot entry = new HotOrNot(agones.this); 

      entry.open(); 

      entry.createEntry(msg.getTitle(), msg.getagonistiki(), msg 
        .getskor(), msg.getgipedo(), msg.getDate(),msg.getgoal1(),msg.getgoal2(),msg.getDescription()); 

      // entry.update(msg.getTitle(),msg.getagonistiki(),msg.getskor(),msg.getgipedo(),msg.getDate()); 

      entry.close(); 

回答

3

試試這個功能::

public void createEntry(String title, String getagonistiki, String getskor, String getgipedo, String date, String getgoal1, String getgoal2, 
     String teliko_skor) { 

    Cursor c = null; 
    boolean isInserted = false; 
    try {  
     c = ourDatabase.rawQuery("select "+DBHelper.TITLE+" from osfpDB", null); 
     if(c != null){ 
      for(int i=0; i<c.getCount();i++){ 
       c.moveToPosition(i); 
       if(c.getString(0).equals(title)){ 
        Log.e("same title text means duplicate :",title+" : "+c.getString(0)); 
        isInserted = true; 
        break; 
       } 
      } 
     } 

     ContentValues cv = new ContentValues(); 
     cv.put(DBHelper.TITLE, title); 
     cv.put(DBHelper.AGONISTIKI, getagonistiki); 
     cv.put(DBHelper.SKOR, getskor); 
     cv.put(DBHelper.GIPEDO, getgipedo); 
     cv.put(DBHelper.DATE, date); 
     cv.put(DBHelper.GOALA, getgoal1); 
     cv.put(DBHelper.GOALB, getgoal2); 
     cv.put(DBHelper.DESCRIPTION, teliko_skor); 

     Log.e("ourDatabase", "" + ourDatabase); 

     if (ourDatabase == null) { 
      ourDatabase = getWritableDatabase(); 
     } 

     if(isInserted){ 
      ourDatabase.update("osfpDB", cv, null, null); 
     }else{ 
      ourDatabase.insert("osfpDB", null, cv); 
     } 
    } catch (Exception e) { 
     Log.e("Exception in insert :", e.toString()); 
     e.printStackTrace(); 
    } 
} 


在你身邊r問題我注意到你只想丟棄重複的記錄,
因爲你不需要更新記錄或不改變你的代碼功能只需將主鍵設置到你的表'標題'列 - 這將不允許插入重複記錄..(如果您有其他查詢告訴我)

ref:Naming conventions