简体   繁体   中英

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!

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"))
                     })

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM