[英]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()
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.在 SQLite 中, SET
语句左侧的更新列不应使用表的名称/别名进行限定。
Also, you should not use the updated table in the FROM
clause.此外,您不应在FROM
子句中使用更新后的表。
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;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.