【问题标题】:Android Notification not inserting into SQLite databaseAndroid 通知未插入 SQLite 数据库
【发布时间】:2015-09-05 19:25:18
【问题描述】:

我已经调试这个问题几个小时了,我环顾四周,似乎找不到解决方案。当我运行代码时,一切正常,除了 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();

}

【问题讨论】:

  • 在插入时有点困惑,您删除表并在插入时在通知表中重新创建另一件事,您也没有提供主键列值
  • 它是键列值。我这样做是为了每次都可以从一个新表开始进行调试
  • categoryIDtagID 变量从哪里来?如果您没有在 updateLaw 方法中将它们作为参数传递?如果您使用空参数调用 getter 方法,如何分配 categoryNametagName 的值?
  • 我没有包括那些正常工作的功能
  • 为什么认为没有插入值?

标签: java android sql sqlite


【解决方案1】:

尝试关闭();然后 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();

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-12-06
    • 2021-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-14
    相关资源
    最近更新 更多