1

I'm having issues with buy. Initially, I was getting a RuntimeError: no such column: user_id At the time, I had the first code as below. After making changes, I now get RuntimeError: Bind parameter 'user_id' without a renderable value not allowed here.

Any help is greatly appreciated.

def buy():
    """Buy shares of stock."""
    if request.method == "POST":
        #require that a user input a stock's symbol. Render and apology if the input is blank
        if not request.form.get("symbol"):
            return apology("Provide a symbol")

        # Render and apology if the input is blank or 
        #the symbol does not exist (as per the return value of lookup).
        symbol = request.form.get("symbol")

        looked_up_symbol = lookup(symbol)
        #You will probably want to call 'lookup' to lookup a stock's current price
        if not looked_up_symbol:
            return apology("Invalid symbol")


        stock_price = looked_up_symbol['price']

        #return render_template("index.html")

        #Require that a user input a number of shares. Render apology if the input is not a positive integer
        no_of_shares = int(request.form.get("shares"))

        if no_of_shares < 0:
            return apology("Invalid number of shares")

        #You will also proabably want to select how much cash the user currently has in users.
        cash_balance = db.execute("SELECT cash FROM users WHERE id = :id", id=session["user_id"])

        #total cost of purchasing the shares
        total_cost = no_of_shares * stock_price


        #Add one or more new tables to finance.db via which to keep track of the purchase. 
        #Store enough information so that you know who bought what at what price and when. 

        #Use appropriate SQLite types.

        #Define 'unique' indexes on any fields that should be unique

        #Define (non-'unique') indexes on any fields via which you will search (as via 'select' with 'where')

        #if total cost of shares is below the balance then make the purchase
        if total_cost <= cash_balance[0]["cash"]:
            user_id = session["user_id"]
            #buy the stock
            cash_remaining = cash_balance[0]["cash"] - total_cost
            db.execute("INSERT INTO transactions (user_id, symbol, stock_price, no_of_shares, total_cost) 
            VALUES (user_id, symbol, stock_price, no_of_shares, total_cost)")


        #Render an apology, without complete a purchase,
        #if the user cannot afford the number of shares at the current price
        else:
            return apology("You can't afford that number of shares at the current price")

    # Don't worry about race conditions (or use transactions)

    #Upon implementing 'buy' correctly, you should be able to see users' purchases
        #in your new table(s) via phpLiteAdmin or 'sqlite3'

    return render_template("buy.html")
    #return apology("TODO")

After researching, I found the following: RuntimeError: no such column: shares

I then changed the db.execute command based on the suggestion made in the post to the following

db.execute("INSERT INTO transactions (user_id, symbol, stock_price, no_of_shares, total_cost) 
VALUES (:user_id, :symbol, :stock_price, :no_of_shares, :total_cost),
user_id=user_id, symbol=symbol, stock_price=stock_price, no_of_shares=no_of_shares, total_cost=total_cost")

Now I get RuntimeError: Bind parameter 'user_id' without a renderable value not allowed here.

1

I solved it using this link Challenge inserting into database from buy() as a resource.

The problem was with the placement of my quotes

I changed

db.execute("INSERT INTO transactions (user_id, symbol, stock_price, no_of_shares, total_cost) 
    VALUES (:user_id, :symbol, :stock_price, :no_of_shares, :total_cost),
    user_id=user_id, symbol=symbol, stock_price=stock_price, no_of_shares=no_of_shares, total_cost=total_cost")

to

db.execute("INSERT INTO transactions (user_id, symbol, stock_price, no_of_shares, total_cost) 
    VALUES (:user_id, :symbol, :stock_price, :no_of_shares, :total_cost)",
     user_id=session["user_id"], symbol=symbol, stock_price=stock_price, no_of_shares=no_of_shares, total_cost=total_cost)

Everything works fine.

2
  • funny how we ask questions but end up fixing them ourselves Jun 19 '17 at 1:41
  • @SameerMalik yea, stepping away from the problem helps sometimes.
    – Salamit
    Jun 19 '17 at 7:51

You must log in to answer this question.

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