'sqlite3.Cursor'对象在Python Flask中没有属性'__getitem__'错误

时间:2016-10-23 21:24:21

标签: python sqlite flask

这是我的代码。每次按下登录时我都会收到此错误:

'sqlite3.Cursor'对象没有属性'__getitem __'

这是我的登录标签:

@app.route('/', methods=['GET', 'POST'])
def login():
    error= None
    if request.method == "POST":
        with sql.connect("database.db") as con:
            cur = con.cursor()
            try:
                data = cur.execute("SELECT name FROM users WHERE name= ?",(request.form['username'],) )
                data.fetchone()

                if sha256_crypt.verify(request.form['password'], data[0]):
                    session['logged_in'] == True
                    session['username'] = request.form('username')
                    flash('Successfully logged in')
                    return redirect(url_for(hello_world))
                else:
                    error = "Invalid Password or User. Try again."

            except Exception as e:
                flash(e)
                return render_template('login.html',error=error)
        con.close()
        gc.collect()
    return render_template('login.html', error=error)

我做错了什么?

1 个答案:

答案 0 :(得分:0)

__getitem__表示方括号访问,在本例中为data[0]。该错误告诉您,您不能使用这样的游标。将data[0]替换为data.fetchone()返回的值。