My buy function is crashing at the SQL query where I'm trying to insert data from the transaction into a transactions table. The types of each column are transaction_id(integer, primary key), user_id(integer), buy/sell(text), symbol(text), name(text), shares(integer), total(numeric(38,2)). All columns are not NULL
The query I am trying to execute is:
"""INSERT INTO transactions (user_id, buy/sell, symbol, name, shares, total) VALUES (1, Buy, 'NFLX', 'Netflix, Inc.', 1, 526.27)"""
Debug50 shows the query in red and the error underneath is:
ERROR:application:Exception on /buy [POST]
Here is my buy function:
def buy(): """Buy shares of stock"""
if request.method == "POST":
# Calling lookup function from helpers.py
# with the symbol provided from the form submission
result = lookup(request.form.get("symbol"))
if not result:
return apology("Please enter a valid symbol")
# Number of shares the user is trying to buy
# Handling invalid inputs
shares = int(request.form.get("shares"))
if not shares or shares <= 0:
return apology("Please enter an integer value of shares")
# Accessing the logged in user's information
buying_user = db.execute("SELECT * from users WHERE id=:user_id", user_id=session["user_id"])
available_cash = buying_user[0]["cash"]
needed_cash = round(result["price"] * shares, 2)
# Handling if the user does not have enough cash
# else, updating users cash and inserting transaction data into transactions table
if needed_cash > available_cash:
return apology("Sorry, you don't have enough cash")
else:
db.execute("UPDATE users SET cash=:cash WHERE id=:user_id",
cash=round(available_cash - needed_cash, 2), user_id=session["user_id"])
db.execute("""INSERT INTO transactions
(user_id, buy/sell, symbol, name, shares, total) VALUES
(:user_id, Buy, :symbol, :name, :shares, :total)""",
user_id=session["user_id"], symbol=result["symbol"], name=result["name"],
shares=shares, total=needed_cash)
return redirect("/")
else:
return render_template("buy.html")
Any ideas please?