CS50 财务:运行时错误:无法在事务中启动事务



所以,我正在尝试做CS50财务任务,对于那些不知道它的人,它是一个烧瓶应用程序,你可以为股票市场报价并购买它们,无论如何我正在尝试设置一条允许用户购买报价股票市场的路线,这是出现问题的代码:

# Adding the values into the purchases table
try:
db.execute("INSERT INTO purchases(user_id, company_symbol, name, shares, price, total_price, transaction_time) VALUES (:user_id, :symbol, :name, :shares, :price, :total_price, :transaction_time)",
user_id=user_id, symbol=quote["symbol"], name=quote["name"], shares=shares, price=quote["price"], total_price=total_price, transaction_time=transaction_time)
except:
value = db.execute("SELECT shares, price, total_price FROM purchases WHERE user_id = :user_id", user_id=user_id)
for item in value:
item["shares"] = item["shares"] + shares
item["total_price"] = item["total_price"] + total_price
db.execute("UPDATE purchases SET shares = :shares, price = :price, total_price = :total_price WHERE user_id = :user_id",
shares=item["shares"], price=quote["price"], total_price=item["total_price"], user_id=user_id)
return render_template("bought.html")

try部分中,我尝试将值INSERT到表购买中并且有效,问题出在expect部分,当用户已经购买相同的报价时调用此部分,它的工作是UPDATE表中的值购买,但是当它运行时它会运行错误。

错误:

RuntimeError: cannot start a transaction within a transaction

以下是完整路线:

@app.route("/buy", methods=["GET", "POST"])
@login_required
def buy():
"""Buy shares of stock"""
if request.method == "GET":
return render_template("buy.html")
else:
# Getting the form values
symbol = request.form.get("symbol")
shares = float(request.form.get("shares"))
quote = lookup(symbol)
# Checking for iput errors
if not symbol:
return apology("MISSING SYMBOL")
elif not shares:
return apology("MISSING SHARES")
elif quote == None:
return apology("INVALID SYMBOL")
# Cheking if affordable
user_id = session["user_id"]
user_wallet = db.execute("SELECT cash FROM users WHERE id = :user_id", user_id=user_id)
total_price = quote["price"] * shares
for wallet in user_wallet:
if total_price > wallet['cash']:
return apology("CAN'T AFFORD")
# Getting the current time
transaction_time = datetime.datetime.now()
# Creating the purchases table if not exist
db.execute("CREATE TABLE IF NOT EXISTS purchases (user_id INTEGER NOT NULL, company_symbol TEXT NOT NULL UNIQUE, name TEXT NOT NULL UNIQUE, shares NUMERIC NOT NULL, price NUMERIC NOT NULL, total_price NUMERIC NOT NULL,transaction_time datetime NOT NULL,FOREIGN KEY (user_id) REFERENCES users(id))")
# Adding the values into the purchases table
try:
db.execute("INSERT INTO purchases(user_id, company_symbol, name, shares, price, total_price, transaction_time) VALUES (:user_id, :symbol, :name, :shares, :price, :total_price, :transaction_time)",
user_id=user_id, symbol=quote["symbol"], name=quote["name"], shares=shares, price=quote["price"], total_price=total_price, transaction_time=transaction_time)
except:
value = db.execute("SELECT shares, price, total_price FROM purchases WHERE user_id = :user_id", user_id=user_id)
for item in value:
item["shares"] = item["shares"] + shares
item["total_price"] = item["total_price"] + total_price
db.execute("UPDATE purchases SET shares = :shares, price = :price, total_price = :total_price WHERE user_id = :user_id",
shares=item["shares"], price=quote["price"], total_price=item["total_price"], user_id=user_id)
return render_template("bought.html")

提前感谢您的帮助。

">当用户已经购买相同的报价时调用此部分"是问题的核心。如果purchases没有主键,则系统无法知道用户已经购买了相同的报价。如果 INSERT 在 UNIQUENESS 约束上失败,则此代码将正常工作。它不可能在唯一性上失败,因为购买没有PK,那么为什么插入失败呢?

最佳猜测:

  • 未创建表,因为它已存在
  • 由于某些架构问题,插入失败,例如无效的列名

故障 排除:

  • except:更改为except Exception as e:
  • print(e)添加为"例外"块中的第一行
  • 发生错误时,请备份烧瓶日志以查看引发的异常
  • 根据需要更正问题

设计: 如果购买表旨在按符号跟踪用户的持股,则该表需要在 user_id,symbol 上有一个主键。需要修改 UPDATE 以适应这种情况(id WHERE user_id = sth AND symbol = sth)。

我无法从技术上解释运行时错误。这是界面的一个怪癖,可以通过纠正潜在问题来纠正。

最新更新