简体   繁体   English

如何更新最新的post sqlite3?

[英]how to update latest post sqlite3?

I need to update the latest entry in the database.我需要更新数据库中的最新条目。 While the query works correctly in the DB browser, it doesn't work in code.虽然查询在数据库浏览器中工作正常,但它在代码中不起作用。 Please tell me what's the matter?请告诉我怎么了?

class MySQL:
    def __init__(self):
        self.connection = sqlite3.connect(file_db, check_same_thread=False)
        self.cursor = self.connection.cursor()
        self.add_bd()     
    def update_sell_price(self, sell_price):
        with self.connection:
            self.cursor.execute(
                f'UPDATE Deals SET sell_price={sell_price}  ORDER BY deal_id DESC LIMIT 1')

Gives an error message: sqlite3.OperationalError: near "ORDER": syntax error给出错误信息:sqlite3.OperationalError: near "ORDER": syntax error

The LIMIT and ORDER BY Clauses are available in the UPDATE statement only if SQLite is built with the SQLITE_ENABLE_UPDATE_DELETE_LIMIT compile-time option, which I suspect is not what you have in the version that you work with.仅当使用SQLITE_ENABLE_UPDATE_DELETE_LIMIT编译时选项构建 SQLite 时, UPDATE语句中的LIMIT 和 ORDER BY 子句才可用,我怀疑这不是您使用的版本中的内容。

Instead use a scalar subquery to get the max deal_id :而是使用标量子查询来获取最大deal_id

sql = """
  UPDATE Deals 
  SET sell_price = ?
  WHERE deal_id = (SELECT MAX(deal_id) FROM Deals)
"""
self.cursor.execute(sql, (sell_price,))

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

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