Query in PostgreSQL returns NoneType

0

After creating a user in my database in my application implemented in Heroku, I enter the registration page if it succeeds. When I try to log in, make the following query:

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

However, I get the following error:

  

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'

However, the registration query was successful and I am pretty sure that I provided the correct credentials. Here is the code involved:

@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']
    
asked by ThePassenger 03.07.2018 в 17:28
source

1 answer

0

The execute method by itself returns nothing, the information is obtained using some of the methods fetch* If you are looking for a single record then the best thing you can do is use fetchone and validate the result:

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

if data:
   password = data['password']
   # ...

The fetchone method will return None if no data is available.

    
answered by 03.07.2018 в 21:36