无法更新 SQLite 上的最后一行

发布于 2024-12-03 00:48:35 字数 1350 浏览 1 评论 0原文

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});  
}

我尝试更新 SQLite 中 KEY_CONTENT5 列的最后一行,但出现错误。 我猜它的错误在 "ORDER_BY("+KEY_ID+") DESC LIMIT 0,1" 但我不知道如何使其正确。如果你知道的话请告诉我。谢谢。

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)

活动类:

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

SQLiteAdapter 类(不是活动):

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 });

    }
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)

Activity 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 });

    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

你穿错了嫁妆 2024-12-10 00:48:35

您无法在更新前下订单。
你可以尝试这样的事情:
WHERE id=(SELECT max(id) FROM TABLE) 如果你想更新最后一个 id,假设你的序列没有被修改。

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.

空名 2024-12-10 00:48:35
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);
    }

我的最终答案。

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.

你是我的挚爱i 2024-12-10 00:48:35

您可以查询 ID,然后更新这一行...如果您的 key_id 值不唯一,则需要使用主键列而不是这一列...

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);
}

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);
}
坐在坟头思考人生 2024-12-10 00:48:35

UPDATE 表集 col = 1 WHERE id = (SELECT MAX(id) FROM 表)

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文