Python 3.x 类型错误:';ResultProxy';对象在尝试在会话中存储用户ID时不支持索引

Python 3.x 类型错误:';ResultProxy';对象在尝试在会话中存储用户ID时不支持索引,python-3.x,session,flask,flask-sqlalchemy,Python 3.x,Session,Flask,Flask Sqlalchemy,在我的Signedup路由中,我试图在会话中存储用户id并自动登录用户,但当我尝试这样做时,我收到以下错误: TypeError:ResultProxy类型的对象不支持索引 并且,在我的登录路径中,我试图记住哪个用户已登录,但在这里我得到错误: TypeError:ResultProxy类型的对象没有len() signeduproute inapplication.py @app.route("/signedup",methods=["GET","POST"]) def signedup():

在我的
Signedup
路由中,我试图在会话中存储用户id并自动登录用户,但当我尝试这样做时,我收到以下错误:

TypeError:ResultProxy类型的对象不支持索引

并且,在我的
登录
路径中,我试图记住哪个用户已登录,但在这里我得到错误:

TypeError:ResultProxy类型的对象没有len()

signedup
route in
application.py

@app.route("/signedup",methods=["GET","POST"])
def signedup():
    if request.method=="POST":
        password = request.form.get("password")
        hash = pwd_context.encrypt(password)


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



        if not name:
            return "must provide username"
        elif not password:
            return "must provide password"



        registration = db.execute("INSERT INTO users (username,password) VALUES (:username,:password)",
                    {"username":name,"password":hash})

        if not registration:
            return "pick a different username"


        # Store their ID in session and log them in automatically

     user_id = db.execute("SELECT user_id FROM users where username = :username",{"username":name})        
            # It means something like - "give me the first row in result

            # and retrieve the value of the key "id"
#ERROR
            session["user_id"] = user_id[0]["id"]      
            db.commit()

            return render_template("success.html",name = name)
        else:
            return render_template("signup.html")
@app.route("/signin",methods=["GET","POST"])
def signin():
    # LOG A USER IN
    #forget any user_id
    session.clear()

    if request.method == "GET":
        return render_template("login.html")

    else:
        user_name = request.form.get("username")

        # ensure username is provided
        if not request.form.get("username"):
            return "must provide username"

        # ensure password is provided
        elif not request.form.get("password"):
            return "must provide password"


        # query the database for the username
        rows = db.execute("SELECT * FROM users where username = :username",{"username":user_name})

        # ensure username exists 
        if len(rows) != 1 or not pwd_context.verify(request.form.get("password"),rows[0]["hash"]):
            return "Invalid username"

        # remember which user has logged in 
        session["user_id"] = rows[0]["id"]
signin
application.py中路由

@app.route("/signedup",methods=["GET","POST"])
def signedup():
    if request.method=="POST":
        password = request.form.get("password")
        hash = pwd_context.encrypt(password)


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



        if not name:
            return "must provide username"
        elif not password:
            return "must provide password"



        registration = db.execute("INSERT INTO users (username,password) VALUES (:username,:password)",
                    {"username":name,"password":hash})

        if not registration:
            return "pick a different username"


        # Store their ID in session and log them in automatically

     user_id = db.execute("SELECT user_id FROM users where username = :username",{"username":name})        
            # It means something like - "give me the first row in result

            # and retrieve the value of the key "id"
#ERROR
            session["user_id"] = user_id[0]["id"]      
            db.commit()

            return render_template("success.html",name = name)
        else:
            return render_template("signup.html")
@app.route("/signin",methods=["GET","POST"])
def signin():
    # LOG A USER IN
    #forget any user_id
    session.clear()

    if request.method == "GET":
        return render_template("login.html")

    else:
        user_name = request.form.get("username")

        # ensure username is provided
        if not request.form.get("username"):
            return "must provide username"

        # ensure password is provided
        elif not request.form.get("password"):
            return "must provide password"


        # query the database for the username
        rows = db.execute("SELECT * FROM users where username = :username",{"username":user_name})

        # ensure username exists 
        if len(rows) != 1 or not pwd_context.verify(request.form.get("password"),rows[0]["hash"]):
            return "Invalid username"

        # remember which user has logged in 
        session["user_id"] = rows[0]["id"]

您可以简单地迭代resultProxy对象

for iter in user_id:
   session["user_id"] = iter[0]

我遇到了相同的错误,通过在db.execute命令末尾添加.fetchone()解决了这个问题:

user_id = db.execute("SELECT user_id FROM users where username = :username",{"username":name}).fetchone()

必须使用.fetchone(返回单个结果)或.fetchall(返回所有结果)才能对SQLAlchemy命令返回的resultProxy对象使用索引。

rly这是必需的。。。世界跆拳道联盟