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 sqlite python table Based on match between another table

I tried to use the following code but it is giving errors:

import sqlite3    
conn = sqlite3.connect('stock.db')  
cursor = conn.cursor()
   

    conn.execute("UPDATE COMSEC_STOCK SET COMSEC_STOCK.quantity = COMSEC_STOCK.quantity -1 FROM COMSEC_STOCK, Comsec_Out_Temp WHERE COMSEC_STOCK.product_id = Comsec_Out_Temp.product_id")
        
    cursor.close()    
    conn.commit()

>Solution :

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

In SQLite, the update column(s) on the left-hand side of the SET statement should not be qualified with the table’s name/alias.

Also, you should not use the updated table in the FROM clause.

Write your code like this:

UPDATE COMSEC_STOCK 
SET quantity = COMSEC_STOCK.quantity -1 
FROM Comsec_Out_Temp 
WHERE Comsec_Out_Temp.product_id = COMSEC_STOCK.product_id;

or, better with aliases for the tables:

UPDATE COMSEC_STOCK AS s
SET quantity = s.quantity -1 
FROM Comsec_Out_Temp AS t 
WHERE t.product_id = s.product_id;
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