def buy():
    """Buy shares of stock"""
    if request.method == "GET":
        return render_template("buy.html")
        if not request.form.get("symbol"):
            return apology("missing symbol", 400)
        if not request.form.get("shares"):
            return apology("missing shares", 400)
        symbol = request.form.get("symbol")
        shares = float(request.form.get("shares"))
        lis = lookup(symbol)
        cost = float(lis["price"])
        price = cost * shares
        row = db.execute("SELECT * FROM users WHERE id = :id", id = session["user_id"])
        saving = row[0]["cash"]
        if price > saving:
            return apology("can't afford", 400)
        saving = saving - price
        db.execute("UPDATE users SET cash=:cash WHERE id=:id", cash = saving, id = session["user_id"])
        db.execute("CREATE TABLE IF NOT EXISTS tracker ('id' INTEGER NOT NULL, 'name' TEXT NOT NULL, 'no' INTEGER NOT NULL)")
        db.execute("ALTER TABLE tracker ADD CONSTRAINT test UNIQUE(id,name)")
        rows = db.execute("SELECT * FROM tracker WHERE id=:id AND name=:name", id=session["user_id"], name=symbol)
        if not rows:
            var = 0
            var = rows[0]["no"]
        db.execute("INSERT INTO tracker (id, name, no) VALUES (:id, :name, :no)", id=session["user_id"], name=symbol, no = var + int(shares))


    ERROR:application:Exception on /buy [POST]
Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 2446, in wsgi_app
    response = self.full_dispatch_request()
  File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1951, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1820, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/usr/local/lib/python3.7/site-packages/flask/_compat.py", line 39, in reraise
    raise value
  File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1949, in full_dispatch_request
    rv = self.dispatch_request()
  File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1935, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/home/ubuntu/finance/helpers.py", line 34, in decorated_function
    return f(*args, **kwargs)
  File "/home/ubuntu/finance/application.py", line 73, in buy
    db.execute("ALTER TABLE tracker ADD CONSTRAINT cons UNIQUE(id,name)")
  File "/usr/local/lib/python3.7/site-packages/cs50/sql.py", line 21, in decorator
    return f(*args, **kwargs)
  File "/usr/local/lib/python3.7/site-packages/cs50/sql.py", line 372, in execute
    raise e
RuntimeError: near "CONSTRAINT": syntax error
  • Please post the complete traceback from the terminal. Aug 26 '20 at 3:53
  • yeah i've made the edit Aug 26 '20 at 6:34

The SQLite does not support adding constraints to existing tables.

The use of an unique index shall deliver the same effect.

CREATE UNIQUE INDEX example ON illustrations (reference)

The documentation for the SQLite specific constraints can be found here

You must log in to answer this question.

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