Flask_mysqldb - MYSQL cursor is throwing an error

I am writing a simple auth service in python using flask and flask_mysqldb . There is an error with the cursor. It is showing cursor is not a known member of “None”

import datetime
import os

import jwt
from flask import Flask, request
from flask_mysqldb import MySQL

server = Flask(__name__)
mysql = MySQL(server)


server.config["MYSQL_HOST"] = os.environ.get("MYSQL_HOST")
server.config["MYSQL_USER"] = os.environ.get("MYSQL_USER")
server.config["MYSQL_PASSWORD"] = os.environ.get("MYSQL_PASSWORD")
server.config["MYSQL_DB"] = os.environ.get("MYSQL_DB")
server.config["MYSQL_PORT"] = os.environ.get("MYSQL_PORT")

# print(server.config["MYSQL_HOST"])
# print(server.config["MYSQL_PORT"])

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

def login():
    auth = request.authorization
    if not auth:
        return "missing credentials",401

    #check db for username and password
    cur = mysql.connection.cursor()
    res = cur.execute(
        "SELECT email,password FROM user WHERE email=%s, (auth.username)"

    if res > 0:
        user_row = cur.fetchone()
        email = user_row[0]
        password = user_row[1]

        if auth.username != email or auth.password != password:
            return "invalid credentials",401
            return createJWT(auth.username,os.environ.get("JWT_SECRET"), True)  # type: ignore
        return "invalid credentials",401

def createJWT(a,b):

if __name__ == "__main__":


I have tried to do this in both a virtual Environment and global environment. I get the same error in both. The required packages have been correctly installed.

The documentation says:

So the reason for the error you’re seeing is that the connection is unsuccessful, and returns None.

1 Like

Thank you so much!!
I will check by passing Mysql credentials.

The error line is cur = mysql.connection.cursor(). The message says that mysql.connection is None. You need to figure out why. Perhaps reread the docs relevant to MySQL and connection. (I have no experience here.)