@app.route("/buy", methods=["GET", "POST"])
def buy():
    """Buy shares of stock"""
    if request.method == "POST":
        if not symbol_lookup:
            return apology("Invalid symbol!")
        elif not request.form.get("symbol"):
            return apology("Symbol cannot be empty!")
        elif not request.form.get("shares"):
            return apology("Number of shares cannot be empty!")
        elif int(request.form.get("shares")) <= 0:
            return apology("Number of shares must be a positive integer!")

        rows_1= db.execute("SELECT cash FROM users WHERE id= :id", id = session["user_id"])
        cash_users = rows_1[0]["cash"]
        cost = symbol_lookup["price"] * int(request.form.get("shares"))
        if cost > cash_users:
            return apology("Insufficient cash!")
            **result_portfolio = db.execute("INSERT INT0 portfolio(id,symbol,price) VALUES(:id,:symbol,:price)",id = session["user_id"], symbol = request.form.get("symbol"), price = symbol_lookup["price"])**
            rows_2 = db.execute("SELECT number FROM portfolio WHERE id= :id AND symbol=:symbol", id = session["user_id"],symbol = request.form.get("symbol"))
            number_stocks = rows_2[0]["number"]
            new_number_stocks = number_stocks + int(request.form.get("shares"))
            new_cash = cash_users - (number_stocks * symbol_lookup["price"])
            users_update = db.execute("UPDATE users SET cash = new_cash WHERE id = :id", id = session["user_id"])
            portfolio_update = db.execute("UPDATE portfolio SET number = new_number_stocks WHERE id = :id", id = session["user_id"])
        return redirect("/")
        return render_template("buy.html")

I am getting the following error (after- flask run, and entering information for "buy" on webpage):

RuntimeError: near "INT0": syntax error [SQL: "INSERT INT0 portfolio(id,symbol,price) VALUES(4,'AAPL',232.07)"] (Background on this error at: http://sqlalche.me/e/e3q8)  

and this error as well(below the above error in the terminal window):

AttributeError: 'RuntimeError' object has no attribute 'name'  

Portfolio is the second table that i have made in the database.
Can someone please help as to why these errors are arising, or what could be done to resolve it?


According to the error message, this is an Operational Error.

Your error message says:

(Background on this error at: http://sqlalche.me/e/e3q8)

That link point to a paragraph describing the Operational error. That might mean that something went wrong during some earlier database call.

I think you should add some error checking after all your database calls.

Apart from that:

I'm not sure whether the way you send your sql to the database is safe. You might have a look at https://xkcd.com/327/

And to quote Cliff B:

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

  • Hey! Thanks for your reply. I still had a doubt regarding what the second error possibly means? The only places where 'name' appears is when one calls 'lookup' or in some other html file when taking input (naming the field and specifying placeholder)
    – user22430
    Oct 4 '18 at 7:43
  • I think the second error AttributeError: 'RuntimeError' object has no attribute 'name' is not about some error in your program; It is probably caused by some error-catching system which is trying to get some more information about the RuntimeError which has occurred. If you can make sure that there is no uncaught RuntimeError, that second error should disappear. Oct 4 '18 at 7:58
  • Though your above explanations are helpful , I still am not able to rectify the error. I just am not able to point out as to which database call/ anything else is resulting in an error. Can you help me point it out so that i may correct it? I say this because my previous implementations of 'register' and 'quote' are working perfectly fine, so there could not be a mistake in that which might be interfering here. I guess the mistake is in the code snippet that i have posted in the question. Can you PLEASE help(sorry, but 'buy' has taken up a lot of time!)
    – user22430
    Oct 4 '18 at 9:27
  • To find out which database call results in an error, you'll have to check for errors after each database call. Just like you should do after (almost) every other I/O call. As soon as you have implemented error checking, your error checks will tell you where the error occurred. Oct 4 '18 at 10:03

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .