2015-09-05 53 views
0

我一直在調試這個問題幾個小時,我環顧四周,似乎無法找到解決方案。當我運行代碼時,一切正常,除了insertNotification沒有向通知表中插入一個新值並且沒有引發異常。爲什麼是這樣?Android通知沒有插入SQLite數據庫

public void updateLaw(int lawID, String newSummary, String newFullText){ 

    int tagID = getTagID(lawID); 
    String tagName = getTagName(tagID); 
    int categoryID = getCategoryID(tagID); 
    String categoryName = getCategoryName(categoryID); 

    openToWrite(); 

     ContentValues contentValues = new ContentValues(); 
     contentValues.put(Constants.KEY_SUMMARY, newSummary); 
     if(newFullText!=null) 
      contentValues.put(Constants.KEY_FULL_TEXT, newFullText); 

     mSqLiteDatabase.update(Constants.TABLE_LAW, contentValues, Constants.KEY_LAW_ID + "=" + lawID, null); 
    close(); 
    try { 
     insertNotification(categoryName, tagName + " has changed in " + getLocationName(getLocationID(lawID))); 
    } 
    catch(Exception e){ 
     exceptionHandler.alert(e, "UpdateLaw()"); 
    } 

} 

public void insertNotification(String type, String text){ 

    openToWrite(); 
    try { 
     mSqLiteDatabase.execSQL("DROP TABLE IF EXISTS notification"); 
     String tableQuery = "CREATE TABLE "+Constants.TABLE_NOTIFICATION+" (\n" + 
      Constants.KEY_NOTIFICATION_ID +" INTEGER PRIMARY KEY AUTOINCREMENT," + 
       Constants.KEY_LAW_ID + " INT,\n" + 
      Constants.KEY_NOTIFICATION_TEXT + " VARCHAR,\n" + 
      Constants.KEY_NOTIFICATION_TIME + " DATETIME DEFAULT CURRENT_TIMESTAMP,\n" + 
      Constants.KEY_NOTIFICATION_STATUS + " VARCHAR\n" + 
      ");\n"; 
     mSqLiteDatabase.execSQL(tableQuery); 
    } 

    catch(Exception e){ 
     exceptionHandler.alert(e, "insertNotification()"); 
    } 

    try { 
     ContentValues contentValues = new ContentValues(); 
     contentValues.put(Constants.KEY_NOTIFICATION_TYPE, type); 
     contentValues.put(Constants.KEY_NOTIFICATION_TEXT, text); 
     contentValues.put(Constants.KEY_NOTIFICATION_STATUS, "unread"); 

     mSqLiteDatabase.insert(Constants.TABLE_NOTIFICATION, null, contentValues); 
    } 

    catch(Exception e){ 
     exceptionHandler.alert(e, "insertNotification()"); 
    } 
    close(); 

} 
+0

有點糊塗了,在插入你刪除表,並在插入您還沒有提供主鍵列值 – Pavan

+0

的時間重新創建在通知表另一件事的時候也是鍵列值。我這樣做,所以我可以每次都從一張新表開始調試 –

+0

如果您沒有在'updateLaw'方法中將'categoryID'和'tagID'變量作爲參數傳入,那麼'categoryID'和'tagID'變量來自哪裏?如果您使用空參數調用getter方法,如何分配'categoryName'和'tagName'的值? – user5292387

回答

-1

try close();然後openToWrite();在插入新記錄之前。

public void insertNotification(String type, String text){ 

openToWrite(); 
try { 
    mSqLiteDatabase.execSQL("DROP TABLE IF EXISTS notification"); 
    String tableQuery = "CREATE TABLE "+ Constants.TABLE_NOTIFICATION + " (" + 
     Constants.KEY_NOTIFICATION_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + 
      Constants.KEY_LAW_ID + " INT, " + 
     Constants.KEY_NOTIFICATION_TEXT + " VARCHAR, " + 
     Constants.KEY_NOTIFICATION_TIME + " DATETIME DEFAULT CURRENT_TIMESTAMP, " + 
     Constants.KEY_NOTIFICATION_STATUS + " VARCHAR " + 
     ")"; 
    mSqLiteDatabase.execSQL(tableQuery); 
} 

catch(Exception e){ 
    exceptionHandler.alert(e, "insertNotification()"); 
} 

try { 
close(); 
openToWrite(); 
    ContentValues contentValues = new ContentValues(); 
    contentValues.put(Constants.KEY_NOTIFICATION_TYPE, type); 
    contentValues.put(Constants.KEY_NOTIFICATION_TEXT, text); 
    contentValues.put(Constants.KEY_NOTIFICATION_STATUS, "unread"); 

    mSqLiteDatabase.insert(Constants.TABLE_NOTIFICATION, null, contentValues); 
} 

catch(Exception e){ 
    exceptionHandler.alert(e, "insertNotification()"); 
} 
close(); 

}