MySQL 数据库查询返回整数而不是行数据

我正在尝试测试以表单提交的信息(电子邮件地址和密码)是否已存在于数据库中。如果电子邮件存在,我想检查数据库中相应的哈希密码是否与提交的密码的哈希版本匹配。


这是我的登录路线的完整摘录:


# Login route

@app.route("/login", methods=["GET", "POST"])

def login():

    """Log user in"""


    # Forget any user_id

    session.clear()


    # User reached route via POST (as by submitting a form via POST)

    if request.method == "POST":


        # Create a cursor

        cur = mysql.connection.cursor()


        # Ensure email was submitted

        if not request.form.get("email"):

            return apology("must provide email", 403)


        # Ensure password was submitted

        elif not request.form.get("password"):

            return apology("must provide password", 403)


        # Store email in a variable

        email = request.form.get("email")


        # Query if email is within database

        query = cur.execute("SELECT * FROM users WHERE email = %s", (email,))


        # Ensure email exists and that the password is correct

        if len(query) != 1 or not check_password_hash(query[0]["hashedpassword"], request.form.get("password")):

            return apology("invalid email and/or password", 403)


        # Remember which user has logged in

        session["user_id"] = query[0]["id"]


        # Redirect user to the homepage

        return redirect("/")


    # User reached route via GET (as by clicking a link or via redirect)

    else:

        return render_template("login.html")

我遇到困难的部分是:


        # Ensure email exists and that the password is correct

        if len(query) != 1 or not check_password_hash(query[0]["hashedpassword"], request.form.get("password")):

            return apology("invalid email and/or password", 403)

在这里,我尝试从数据库中提取指定电子邮件地址的单行信息。我试图使用 len 来查看是否存在单行(不能存在两行,因为我已确保电子邮件地址列在数据库中是唯一的),因此应该只有单行或没有。


梵蒂冈之花
浏览 75回答 1
1回答

慕雪6442864

你缺少的部分是rows = cursor.fetchall()它将返回数据给您,您将能够在其上实现您的逻辑rows
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python