我的代码工作正常,直到我决定将SQLite数据库中的一个表重命名为transaction
而不是portfolio
。然后它只是给我服务器错误,甚至没有重定向。当它最初命名为portfolio
时,我能够在我的数据库中正确更新详细信息。但是,现在当我运行下面的时候,它一直给我一个语法错误:
错误:靠近“交易”:语法错误。
从字面上看,我所改变的只是portfolio
到transaction
。表名匹配。
db.execute("INSERT INTO transaction (symbol, quantity, price,user_id) VALUES (:symbol, :quantity, :price, :user_id)", symbol=stock["symbol"], quantity=shares, price=stock["price"], user_id=session["user_id"])
不确定错误是什么。
完整的代码段:
if request.method == "POST":
stock = lookup(request.form.get("symbol"))
if not stock:
return apology ("Please enter a valid symbol")
try:
shares = int(request.form.get("shares"))
if not shares or shares <= 0:
return apology("Please enter a valid amount")
except:
return apology("Please enter a valid amount")
user_cash = db.execute("SELECT cash FROM users WHERE id=:user_id;", user_id=session["user_id"])
user_cash = int(user_cash[0]["cash"])
if shares*stock["price"] > user_cash:
return apology("Sorry, not enough cash")
else:
db.execute("INSERT INTO transaction (symbol, quantity, price, user_id) VALUES (:symbol, :quantity, :price, :user_id)", symbol=stock["symbol"], quantity=shares, price=stock["price"], user_id=session["user_id"])
db.execute("UPDATE users SET cash = cash -:purchase WHERE id = :user_id", purchase = shares*stock["price"], user_id=session["user_id"])
return redirect("/")
else:
return render_template("buy.html")