开发者

Cannot update last row on SQLite

开发者 https://www.devze.com 2023-04-02 02:14 出处:网络
public public udpateNoteInfo(String text){ ContentValues val = new ContentValues(); val.put(KEY_CONTENT5, text);
public public udpateNoteInfo(String text){
    ContentValues val = new ContentValues();
    val.put(KEY_CONTENT5, text);        

    sqLiteDatabase.update(MYDATABASE_TABLE, val,"ORDER_BY("+KEY_ID+") DESC LIMIT 0,1",  new String[]{text});  
}

I try to update the last row of the KEY_CONTENT5 column in my SQLite, but it's error. I guess its mistake at "ORDER_BY("+KEY_ID+") DESC LIMIT 0,1" but I don't know how to make it correct. Please tell me if you know that. Thank you.

ERROR:
09-05 11:47:54.769 E/Database( 4386): Error updating note=Test using UPDATE PERSONAL_TABLE SET note=? WHERE _id = (SELECT max(_id) FROM PERSONAL_TABLE)

Acti开发者_开发问答vity class:

public void updateNote(String txt) {
        mySQLiteAdapter = new PersonalSQLiteAdapter(this);
        mySQLiteAdapter.openToWrite();
        cursor = mySQLiteAdapter.queueAll();
        if (cursor != null) {   
            mySQLiteAdapter.udpateNoteInfo(txt);
        }
        mySQLiteAdapter.close();
    }

SQLiteAdapter class (not activity):

public void udpateNoteInfo(String text) {
        ContentValues val = new ContentValues();
        val.put(KEY_CONTENT5, text);
        sqLiteDatabase.update(MYDATABASE_TABLE, val, KEY_ID + " = (SELECT max("
                + KEY_ID + ") FROM " + MYDATABASE_TABLE + ")",
                new String[] { text });

    }


You can't put an order by in an update. You can try something like this: WHERE id=(SELECT max(id) FROM TABLE) if you want to update the last id, assuming your sequences aren't modified.


public void udpateNoteInfo(String text) {
        ContentValues val = new ContentValues();
        val.put(KEY_CONTENT5, text);
        sqLiteDatabase.update(MYDATABASE_TABLE, val, KEY_ID+" = (SELECT max("+KEY_ID+") FROM "+MYDATABASE_TABLE+")", null);
    }

My final answer.


You can query the ID, and then, update this row... if your key_id values are not unique, you'll need to use your primary key column(s) instead of this one...

Cursor cLast = db.query(MYDATABASE_TABLE, [KEY_ID], null, null, null, "ORDER_BY("+KEY_ID+") DESC", "LIMIT 0,1");
if (cLast.moveToFirst()) {
  long lastKey = cLast.getLong(0); // if it's not a long, use the appropriate getter
  sqLiteDatabase.update(MYDATABASE_TABLE, val, "WHERE KEY_ID=?", lastKey);
}


UPDATE table set col = 1 WHERE id = (SELECT MAX(id) FROM table)

0

精彩评论

暂无评论...
验证码 换一张
取 消