Attributeerror: ‘nonetype’ object has no attribute ‘cursor’ [SOLVED]

Discover the solution for the attributeerror: ‘nonetype’ object has no attribute ‘cursor’ error message. In this article, we are going to explain in detail about a ‘”nonetype’ object has no attribute ‘cursor.”

If you’re struggling to figure out how to fix this error, this article will definitely help you get rid of this error. And continue working with your code, especially if you’re new to this error.

We understand that it is quite frustrating to deal with, but don’t worry because it is easy to fix if you fully understand what this ‘nonetype’ object has no attribute ‘cursor error is all about.

Aside from the solutions, we’ll also discuss what this error is all about and the reasons why it occurs.

What is the ‘nonetype’ object in Python?

The ‘nonetype’ object in Python is a special data type that represents the absence of a value. It is commonly used as a default return value for functions that do not return any specific value, and as a placeholder value for uninitialized variables or objects.

What is “attributeerror: ‘nonetype’ object has no attribute ‘cursor'”?

The attributeerror: ‘nonetype’ object has no attribute ‘cursor’ is an error message that occurs when you are trying to call the cursor() method on a None object in Python.

In addition to that, this error is often encountered when there is an issue with the database connection, and the cursor object has not been properly initialized or created.

It can also occur if the cursor object has been deleted or closed before calling the cursor() method.

What are the root cause of this error?

The following are the most common causes of the error:

  • Improper connection to the database. Certainly, an error can occur if your code isn’t properly connecting to the database. If the connection fails, the cursor method won’t work, leading to the attribute error.
  • If your SQL query is incorrect or has a syntax error, it can lead to the ‘nonetype’ object has no attribute ‘cursor’ error.
  • If you’re using the wrong object to call the cursor method. Make sure that the object you’re using is the correct one.

How to fix “attributeerror: ‘nonetype’ object has no attribute ‘cursor'”

Time needed: 2 minutes

Here are the following solutions that will help you easily fix the error:

  1. Check your database connection

    If your code isn’t properly connecting to the database, it’s likely that you’ll encounter this error.
    You should check your database connection and make sure it is established properly before attempting to call the cursor() method

  2. Verify your SQL query

    Double-check your query and make sure that it’s correct.

  3. Check the object

    Make sure that you’re using the correct object to call the cursor method.

Here’s a sample code that can help solve the attributeerror: ‘nonetype’ object has no attribute ‘cursor’ error:

from flask import Flask, render_template, request, flash, redirect, url_for
from flask_mysqldb import MySQL
from passlib.hash import sha256_crypt

app = Flask(__name__)

# MySQL configurations
app.config['MYSQL_HOST'] = 'localhost'
app.config['MYSQL_USER'] = 'root'
app.config['MYSQL_PASSWORD'] = 'password'
app.config['MYSQL_DB'] = 'mydatabase'
app.config['MYSQL_CURSORCLASS'] = 'DictCursor'

# Initialize MySQL
mysql = MySQL(app)

# Initialize passlib
hash = sha256_crypt

# Route for registering a user
@app.route('/register', methods=['GET', 'POST'])
def register():
    form = RegisterForm(request.form)
    if request.method == 'POST' and form.validate():
        name =
        email =
        username =
        password = hash.encrypt(str(

            # Create connection
            conn = mysql.connect()

            # Create cursor
            cur = conn.cursor()

            # Execute query
            cur.execute("INSERT INTO users(name, email, username, password) VALUES(%s, %s, %s, %s)",
            [name, email, username, password])

            # Commit to database

            # Close cursor and connection

            flash('You are now registered and can log in', 'success')

            return redirect(url_for('login'))

        except Exception as e:
            flash('An error occurred while registering: {}'.format(str(e)), 'danger')
            return redirect(url_for('register'))

    return render_template('register.html', form=form)

By ensuring that we have a valid database connection before attempting to use it, we can avoid theattributeerror: ‘nonetype’ object has no attribute ‘cursor’ error and other similar errors that can occur when working with databases in Python.

Related Articles for Python Errors


By executing the different solutions that this article has given, you can easily fix the attributeerror: ‘nonetype’ object has no attribute ‘cursor’ error message in Python.

We are hoping that this article provides you with a sufficient solution; if yes, we would love to hear some thoughts from you.

Thank you very much for reading to the end of this article. Just in case you have more questions or inquiries, feel free to comment, and you can also visit our website for additional information.

Leave a Comment