【发布时间】: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();
}
【问题讨论】:
-
在插入时有点困惑,您删除表并在插入时在通知表中重新创建另一件事,您也没有提供主键列值
-
它是键列值。我这样做是为了每次都可以从一个新表开始进行调试
-
categoryID和tagID变量从哪里来?如果您没有在updateLaw方法中将它们作为参数传递?如果您使用空参数调用 getter 方法,如何分配categoryName和tagName的值? -
我没有包括那些正常工作的功能
-
为什么认为没有插入值?