1

我正在我的数据库助手类中执行更新方法。但我无法解决我在哪里做错了。日志中没有错误,但我在表中的字段没有更新。

public void updateJSON(long id, String newString) {
    ContentValues dataToInsert = new ContentValues();
    dataToInsert.put("cityJSON", newString);
    String where = "id=?";
    String[] whereArgs = new String[]{String.valueOf(id)};
    this.db.update(TABLE_NAME, dataToInsert, where, whereArgs); 
}

我正在传输 json 字符串(带有逗号、括号等)和字段的 ID。

4

4 回答 4

0

也许你想试试这段代码:String mString = Long.toString(id);

于 2012-12-07T11:47:27.883 回答
0

代替

String[] whereArgs = new String[]{String.valueOf(id)};

尝试

String[] whereArgs = { Long.valueOf(id).toString() };

这对我行得通。

于 2012-12-07T11:47:51.800 回答
0

db.update(...)方法返回受影响的行数。您可以在日志中显示此值。如果受影响的行数为零:

  1. 也许行名有问题?
  2. 也许db声明为readableDatabase而不是writableDatabase?我的意思是

SQLiteDatabase db = getReadableDatabase();

而不是getWritableDatabase()

编辑:

您可以编写帮助函数,以 100% 确保该行(具有您需要的 id)确实存在于数据库中:

public boolean isItemExistsInDB( int yourId){
    String selectQuery = "SELECT  * FROM " + 
                         TABLE_NAME+ " 
                         WHERE " + " id = '" + yourId+ "'";

    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);

    //count of rows with "id=yourId"
    int count= cursor.getCount();

    Log.d(getClass().getSimpleName(), "count="+count);

    if(count>0){
        return true;
    }
    else{
         //there is no item with "id=yourId"
        return false;
    }
}
于 2012-12-07T11:55:14.667 回答
0

我认为使用此代码,

this.db.update(TABLE_NAME, dataToInsert, COLUMNNAME + "=?" ,
            new String[]{String.valueOf(id)});
于 2012-12-07T12:09:04.840 回答