0

While implementing "Register" part in PSET8, I ran into the following problem - the code below returns: "TypeError: execute() takes 2 positional arguments but 3 were given".

username = request.form.get("username")
password = request.form.get("password")
password_hash = generate_password_hash(password)
db.execute(
    "INSERT INTO users (username, hash) \
    VALUES(username = :username, password_hash = :password_hash)",
    (username, password_hash))

I tried to re-write this code in a lot of options, searched through similar questions, read the manual for execute, but with no result. Tried second agrument as a list [username, password_hash] and as a dictionary {"username": username, "password_hash": password_hash}

What are 3 arguments? 1st - is SQL command string, 2-nd - the iterable with 2 values inside. What is 3-d in this case? Could you please give me any clue? Thanks.

1

The VALUES and parameters are switched up.

The VALUES clause should be VALUES (:username,:password) The parameters would be username = username, password = password.

| improve this answer | |
  • Thanks! I tried, it causes syntax error pointing to ":" before username in parameters... db.execute("INSERT INTO users (username, hash) VALUES (:username, :password_hash)", (:username = username, :password_hash = password_hash)) – RomanBlade Feb 3 '19 at 17:16
  • Sorry, my fault. Now I have written: db.execute("INSERT INTO users (username, hash) VALUES (:username, :password_hash)", (username = username, password_hash = password_hash)) Result is the same - invalid sintax message which points to first "=" in parameters "username = username" – RomanBlade Feb 3 '19 at 17:32
  • Ah, finally I got it - removed brackets enclosing parameters block and it works! Many thanks! – RomanBlade Feb 3 '19 at 17:38

You must log in to answer this question.

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