0

I'm doing Pset09 "Finance" and I have been stuck in the "index" function for quite a while. I tried to use the already existing "trans" table but I am getting an error. Here is the code:

    @app.route("/")
    @login_required
    def index():
    """Show portfolio of stocks"""
    users = db.execute("SELECT cash FROM users WHERE id = :user_id", user_id=session["user_id"])
    stocks = db.execute(
        "SELECT SUM(shares) FROM purchases WHERE username = :user_id", user_id=session["user_id"])
    company = db.execute("SELECT company FROM purchases WHERE username = :user_id GROUP BY company", user_id=session["user_id"])
    db.execute("INSERT INTO trans (id,stocks,company) VALUES(?,?,?)", session["user_id"],stocks,company)
    trans = db.execute("SELECT * FROM trans")
    return render_template("index.html", trans=trans)

The Error generated is this

    INFO: 192.168.60.0 - - [01/Apr/2021 10:36:39] "GET /static/styles.css HTTP/1.0" 200 -
    INFO: 192.168.30.188 - - [01/Apr/2021 10:36:43] "GET /login HTTP/1.0" 200 -
    INFO: 192.168.30.188 - - [01/Apr/2021 10:36:44] "GET /static/styles.css HTTP/1.0" 200 -
    DEBUG: SELECT * FROM users WHERE username = 'abduladhlazeez_final'
    INFO: 192.168.51.80 - - [01/Apr/2021 10:36:54] "POST /login HTTP/1.0" 302 -
     DEBUG: SELECT cash FROM users WHERE id = 18
    DEBUG: SELECT SUM(shares) FROM purchases WHERE username = 18
    DEBUG: SELECT company FROM purchases WHERE username = 18 GROUP BY company
    ERROR: Exception on / [GET]
    Traceback (most recent call last):
    File "/usr/local/lib/python3.9/site-packages/flask/app.py", line 2447, in wsgi_app
    response = self.full_dispatch_request()
    File "/usr/local/lib/python3.9/site-packages/flask/app.py", line 1952, in full_dispatch_request
    rv = self.handle_user_exception(e)
    File "/usr/local/lib/python3.9/site-packages/flask/app.py", line 1821, in handle_user_exception
    reraise(exc_type, exc_value, tb)
     File "/usr/local/lib/python3.9/site-packages/flask/_compat.py", line 39, in reraise
    raise value
    File "/usr/local/lib/python3.9/site-packages/flask/app.py", line 1950, in full_dispatch_request
    rv = self.dispatch_request()
    File "/usr/local/lib/python3.9/site-packages/flask/app.py", line 1936, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
    File "/home/ubuntu/pset9/finance/helpers.py", line 34, in decorated_function
    return f(*args, **kwargs)
    File "/home/ubuntu/pset9/finance/application.py", line 62, in index
    db.execute("INSERT INTO trans (id,stocks,company) VALUES(?,?,?)", session["user_id"],stocks,company)
    File "/usr/local/lib/python3.9/site-packages/cs50/sql.py", line 21, in decorator
    return f(*args, **kwargs)
    File "/usr/local/lib/python3.9/site-packages/cs50/sql.py", line 177, in execute
    _args = ", ".join([str(self._escape(arg)) for arg in args])
    File "/usr/local/lib/python3.9/site-packages/cs50/sql.py", line 177, in <listcomp>
    _args = ", ".join([str(self._escape(arg)) for arg in args])
    File "/usr/local/lib/python3.9/site-packages/cs50/sql.py", line 472, in _escape
    return sqlparse.sql.TokenList(sqlparse.parse(", ".join([str(__escape(v)) for v in value])))
    File "/usr/local/lib/python3.9/site-packages/cs50/sql.py", line 472, in <listcomp>
    return sqlparse.sql.TokenList(sqlparse.parse(", ".join([str(__escape(v)) for v in value])))
    File "/usr/local/lib/python3.9/site-packages/cs50/sql.py", line 468, in __escape
    raise RuntimeError("unsupported value: {}".format(value))
    RuntimeError: unsupported value: {'SUM(shares)': 9}
    INFO: 192.168.51.80 - - [01/Apr/2021 10:36:55] "GET / HTTP/1.0" 500 -
    INFO: 192.168.51.80 - - [01/Apr/2021 10:36:55] "GET /static/styles.css HTTP/1.0" 200 -

Any help would be appreciated. Stuck on a few days with this. Apparently all the values returned by the SELECT query for "company" etc give the same error.

1

From the doc for execute in CS50 Library for Python

Returns

for SELECT, a list of dict objects, each of which represents a row in the result set

The argument to the INSERT query needs to be a specific element from the SELECT result, eg

stocks[0]['SUM(shares)']
4
  • Thanks. I will try this out!
    – Abdul
    Apr 2 at 2:15
  • I'm getting another error
    – Abdul
    Apr 2 at 2:35
  • company[0]['symbol'] I'm getting this table trans has no column named symbol
    – Abdul
    Apr 2 at 2:39
  • Thanks for the help!
    – Abdul
    Apr 5 at 15:58

You must log in to answer this question.

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