简体   繁体   English

安卓 sqlite更新最后一个插入行

[英]Android. sqlite update last inserte row

I want to update the last inserted row, but I get an error: 我想更新最后插入的行,但出现错误:

08-20 12:08:23.091: E/AndroidRuntime(13598): android.database.sqlite.SQLiteException: near "ORDER": syntax error (code 1): , while compiling: UPDATE REVIEW SET json=? 08-20 12:08:23.091:E / AndroidRuntime(13598):android.database.sqlite.SQLiteException:near“ ORDER”:语法错误(代码1):,同时编译:UPDATE REVIEW SET json =? WHERE ORDER BY date DESC LIMIT 1 日期按日期下限1

My code: 我的代码:

public int updateDataJSON(Review review) {
    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues values = new ContentValues();
    values.put(KEY_JSON, review.getJson());

    return db.update(TABLE_REVIEW, values, " ORDER BY date DESC LIMIT 1", null);
}

ORDER BY and such are only valid in SELECT queries, not UPDATE s. ORDER BY等仅在SELECT查询中有效,而在UPDATE无效。

Consider the following: 考虑以下:

  1. Store the last inserted row id as returned by a call to insert() : 存储对insert()的调用返回的最后插入的行ID:

     long lastInsertRowId = db.insert(...); 
  2. Use the rowid in the update like 在更新中使用rowid

     db.update(TABLE_REVIEW, values, "ROWID=?", new String[] { String.valueOf(lastInsertRowId)2 }); 

You have to read the last date in a separate step: 您必须在单独的步骤中读取最后一个日期:

db.update(..., "date = (SELECT max(date) FROM ReviewTable)", ...);

(If there are multiple rows with the same date value, this will update all of them.) (如果有多个具有相同date值的行,则将更新所有这些行。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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