查询PostgreSQL返回NoneType

时间:2018-07-03 16:03:24

标签: mysql postgresql

在Heroku上部署的应用程序中的数据库中创建用户后,如果成功,我将进入日志页面。当我尝试登录时,它会执行以下查询:

result = cur.execute("SELECT * FROM users WHERE username = %s", [username])

但是它给了我以下错误:

018-07-03T15:02:47.167067+00:00 app[web.1]: File "app.py", line 140, in login
2018-07-03T15:02:47.167069+00:00 app[web.1]: if result > 0:
2018-07-03T15:02:47.167071+00:00 app[web.1]: TypeError: '>' not supported between instances of 'NoneType' and 'int'

但是,注册查询成功,并且我确定我提供了正确的凭据。这是涉及的代码:

@app.route('/login', methods=['GET', 'POST'])
def login():
    if request.method == 'POST':
        # Get Form Fields
        username = request.form['username']
        password_candidate = request.form['password']

        # Create cursor
        #cur = mysql.connection.cursor()
        cur = conn.cursor()

        # Get user by username
        result = cur.execute("SELECT * FROM users WHERE username = %s", [username])

        if result > 0:
            # Get stored hash
            data = cur.fetchone()
            password = data['password']

            # Compare Passwords
            if sha256_crypt.verify(password_candidate, password):
                # Passed
                session['logged_in'] = True
                session['username'] = username

                flash('You are now logged in', 'success')
                return redirect(url_for('dashboard'))
            else:
                error = 'Invalid login'
                return render_template('login.html', error=error)
            # Close connection
            cur.close()
        else:
            error = 'Username not found'
            return render_template('login.html', error=error)

也许这是因为该查询曾经用于MySQL数据库

1 个答案:

答案 0 :(得分:0)

该查询看起来不会返回整数。

也许您想选择COUNT(*)而不是*来获取匹配的行数。