Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
483 views
in Technique[技术] by (71.8m points)

python - Got the error TypeError: execute() takes no keyword arguments

I am trying to build a register/login system with python and sqlite3. It seems to be there is something wrong here in my code.

        if request.form.get("password") != request.form.get("confirm"):
            return apology(" Passwords doesn't match")

#       try:

        prim_key = c.execute("INSERT INTO accounts (username, hash) VALUES (:username, :hash)",
                                 username = request.form.get("username"),
                                 hash = generate_password_hash(request.form.get("password")))
        conn.commit()

#       except:
#           return apology("Sorry! username already exists!", 403)
#       if prim_key is None:
#           return apology("Failed to register! something went wrong!", 403)
#       user_if = prim_key
        return redirect("/")
    else:
        return render_template("register.html")

I commented some lines to figure out if what is wrong. Before quoting I kept getting the error "Sorry! username already exists!".

Now after quoting those lines when I run the application I get

File "application.py", line 76, in register prim_key = c.execute("INSERT INTO accounts (username, hash) VALUES (:username, :hash)", TypeError: execute() takes no keyword arguments

I wonder if I am missing, something these are the modules I imported.

import sqlite3
from flask import Flask, flash, jsonify, redirect, render_template, request, session
from werkzeug.security import check_password_hash, generate_password_hash
import secrets

And if I can't use keyword arguments here, is there a way around this? Or am I doing the entire thing wrong? I am new to Python so I don't know if this is just a dumb question. I read almost all the questions like this one, but I couldn't figure any way to get this done. So any help is appreciated. Thank you for your time!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

As the error states, execute doesn't take any keyword arguments. You need to just pass a dict of values instead:

prim_key = c.execute("INSERT INTO accounts (username, hash) VALUES (:username, :hash)",
                     { 'username': request.form.get("username"), 
                       'hash': generate_password_hash(request.form.get("password"))
                     })

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...