尝试根据值删除 sqlite 中的行时出现内部服务器错误

时间:2021-03-15 23:04:28

标签: python sqlite flask

[enter image description here

我正在尝试创建一个用于买卖股票的应用。如果我出售同一家公司的所有股票,我想删除该行而不是显示 0 股。 我不确定如何根据其值删除该行。

这里给我错误不完整的输入:

localhost/~username/

这行代码在我添加删除条件之前是有效的。

db.execute("INSERT INTO stocks (user_id, symbol, name, shares, price, total_cost, transaction_type) VALUES(:user_id, :symbol, :name, :shares, :price, :total_cost, :transaction_type", user_id=user_id, name=name, symbol=symbol, shares= -1*shares, price=price, total_cost=total_cost, transaction_type=transaction_type)

1 个答案:

答案 0 :(得分:0)

一个问题是执行顺序。这个

db.execute("DELETE FROM stocks WHERE user_id = :user_id AND symbol = :symbol",
                          symbol=symbol, user_id=session["user_id"])

如果股票变为 0,则删除所有股票行。

随后,

db.execute("INSERT INTO stocks (user_id, symbol, name, shares, price, total_cost, transaction_type) VALUES(:user_id, :symbol, :name, :shares, :price, :total_cost, :transaction_type", user_id=user_id, name=name, symbol=symbol, shares= -1*shares, price=price, total_cost=total_cost, transaction_type=transaction_type)

插入具有负份额的行。

看起来股票表为每笔交易保留一行,而不是每只股票一行。这对历史路线很有用。不要删除任何行。

一个解决方案是修改,可能是在 index.html 中。假设一个带有 sum(shares) 的 SELECT,添加 a HAVING clause 以过滤出 sum(shares) 为 0 的行。