So I have this code that's going to check if the name of the company and the user-id are repeated he will just increment the shares but if one of them is new(new user or new company investment) it will insert a new row with this new informations so the next time it will be just incremented so that all that it does + I use a database and I talk with it with some python instructions. note: I use a flask framework
if float(HowMuchHeGot) - HowMuchPrice < 0:
return apology("it's more than you have", 403)
else:
db.execute("UPDATE users SET cash=:howmuchnow WHERE id = :id", howmuchnow = float(HowMuchHeGot) - HowMuchPrice, id=int(session["user_id"]))
rows = db.execute("SELECT * FROM purshases WHERE userid = :userid AND Symbol = :symbol", userid=int(session["user_id"]), symbol= request.form.get("symbol") )
howmuchshareswas = db.execute("SELECT shares FROM purshases WHERE userid = :id AND Symbol = :symbol", id=int(session["user_id"]), symbol=request.form.get("symbol"))
howmuchshareswas = howmuchshareswas[0]["shares"]
howmuchsharesnow = int(request.form.get("shares"))+int(howmuchshareswas)
if len(rows) == 1:
db.execute("UPDATE purshases SET shares=:shares WHERE userid = :id AND Symbol = :symbol", shares=howmuchsharesnow, id=int(session["user_id"]), symbol=request.form.get("symbol"))
else:
db.execute("INSERT INTO purshases (userid, Symbol, Name, shares ) VALUES (:userid, :symbol, :name, :shares)", userid=int(session["user_id"]), symbol=request.form.get("symbol"), name=lookup(request.form.get("symbol"))["name"], shares=int(request.form.get("shares")) )
return redirect("/")
now the problem is that the first if when it will be just incremented is fine but when testing the other conditions(when it will insert a new row) I get an internal server error so there is some problem with this part I guess
else:
db.execute("INSERT INTO purshases (userid, Symbol, Name, shares ) VALUES (:userid,:symbol, :name, :shares)", userid=int(session["user_id"]), symbol=request.form.get("symbol"),name=lookup(request.form.get("symbol"))["name"], shares=int(request.form.get("shares")) )
so if could someone help with this.
Aucun commentaire:
Enregistrer un commentaire