Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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.

Instead use a scalar subquery to get the max deal_id:

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

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading