简体   繁体   中英

Android Java SQLite not updating?

I am going nuts over here. I have been trying to update one column in one row inside of my SQLite Database but its just not happening regardless of what I try.The value is never updated and there are no exceptions in my log.

The below code might summarise my problem best. the updateLatestMessage() function is part of a Database Controller Class.

  public void updateLatestMessage(int messageID, int chatID){
    Log.d("DB", "message ID =" + messageID); //example output = 125
    Log.d("DB", "chat ID =" + chatID); //example output = 2

    SQLiteDatabase db = this.getWritableDatabase();

    try {
        ContentValues cv = new ContentValues();
        cv.put("latest_message_id", messageID);
        db.update("chat", cv, "chat_id = ?" + chatID, null);
        db.close();
        //db.update(tableChat.TABLE_NAME, cv, tableChat.getChatId() + " = ?", new String[]{String.valueOf(chatID)}); // tried this before didn't work either


    }
    catch(Exception e) {
        Log.d("DB", "Error: "+e.toString());
    }

}

currently latest message has the ID of 5

        myDB.chat.updateLatestMessage((int) messageID, globalChatID);//calling the above DB update code

after running this code the latest message still has the id of 5 instead of 125

I am expecting the messageID to be updated inside of the the latest_message_id column inside my already existing chat row.

If the name of the table is "chat" , the name of the column in the WHERE clause is "chat_id" and the argument of WHERE clause is chatID , then the recommended way for the update is this:

int result = db.update("chat", cv, "chat_id = ?", new String[]{String.ValueOf(chatID)});

Check the value of result after the statement.
If its value is greater than 0 then the update was successful.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM