簡體   English   中英

Android Notification未插入SQLite數據庫

[英]Android Notification not inserting into SQLite database

我已經調試了這個問題好幾個小時,並且四處張望,似乎找不到解決方案。 當我運行代碼時,一切正常,但insertNotification不會在通知表中插入新值,並且不會引發任何異常。 為什么是這樣?

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();

}

嘗試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();

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM