Greetings I am having trouble understanding how tables in sql work in buy. So from reading other materials (pset7 buy - ON DUPLICATE KEY syntax question) am I suppose to always make a new row even though the bought stock is the same as another one and just total the columns that need to be totaled up at the end and use that value to be displayed on the html page instead of updating the tables? Something that is also not working when I try to insert it is the current date and time does anyone know what I am doing wrong?
def buy():
"""Buy shares of stock."""
# if user reached route via POST (as by submitting a form via POST)
if request.method == "POST":
# ensure symbol was submitted
quote = lookup(request.form.get("symbol"))
if not request.form.get("symbol") or quote == None:
return apology("must provide symbol/symbol does not exist")
#ensure shares was submitted as positive integer
number = request.form.get("shares")
try:
number = int(number)
if number < 0: #if not a positive int print message and ask for input again by apology
return apology("must provide shares as a positive integer")
except ValueError:
return apology("must provide shares as a integer")
#select how much cash the logged on use has and execute it on the portfolio table
usercash = db.execute("SELECT * FROM users WHERE id = :id", id = session["user_id"])
price =quote.get("price")
symbol = quote.get("symbol")
name = quote.get("name")
stockt = price * number
cash = usercash[0]["cash"]
now = datetime.datetime.now()
#check if user has enough cash for beginning
if stockt > cash:
return apology("Not enough minerals")
#in portfolio insert information using values from user and make new rows of whatever was bought
db.execute("INSERT INTO portfolio (user_id, Symbol, Name, Shares, Price, Total, Datetime) VALUES(:id, :symbol, :name, :shares, :price, :total, :t)"
,id = session["user_id"], symbol = symbol, name = name, shares = number, price = price, total = stockt, t=now)
#add up all the totals to put into the printed html row that is actualy shown
#db.execute("SELECT SUM(Shares) FROM portfolio WHERE symbol = :same", same = symbol)
#render template with new cash total and updated values
return render_template("portfolio.html", sym=symbol, )
return render_template("buy.html")