我正在尝试在我的网站上设置一个注册函数,但我坚持设置查询数据库的逻辑以查看用户名是否存在。如果是这样,它应该返回道歉页面。
这是我的代码:
@app.route("/register", methods=["GET", "POST"])
def register():
"""Register user"""
# User reached route via POST (as by submitting a form via POST)
if request.method == "POST":
# Stored POST data to variables
username = request.form.get('username')
password = request.form.get('password')
confirmation = request.form.get('confirmation')
# Ensure username was submitted
if not username:
return apology("must provide username", 403)
# Ensure password and confirmation were both submitted
elif not password:
return apology("must provide password in both fields", 403)
# Ensure passwords match
elif confirmation != password:
return apology("passwords do not match", 403)
elif db.execute('SELECT id FROM users WHERE username = ?',
(username,)).fetchone() is not None:
return apology("Username already exists", 403)
我不断收到此错误:
elif db.execute('SELECT id FROM users WHERE username = ?',
(username,)).fetchone() is not None:
TypeError: execute() takes 2 positional arguments but 3 were given
我见过他们第一次使用 cursor(( 方法的代码,但我认为 execute(( 方法在 python 3 中这样做?请您协助我的数据库查询,谢谢。
我意识到问题源于我的数据库连接。 所以 .execute(( 语法与标准文档不同。
我更改了以下内容:
from cs50 import SQL; and
db = SQL("sqlite:///finance.db")
自:
import sqlite3; and
db = sqlite3.connect('finance.db')
希望这能帮助世界上的其他人做同样的 pset,不想使用缩写的 cs50 库。我上面的查询语句的语法是正确的。
elif db.execute('SELECT id FROM users WHERE username = ?',
(username,)).fetchone() is not None:
return apology("Username already exists", 403)