0

Code

For a cursor-query:

import mysql.connector
from mysql.connector import errorcode

config = {
    'user': 'root',
    'password': 'root',
    'host': 'localhost',
    'database': 'myDatabase'}


cn = mysql.connector.connect(**config)
cur = cn.cursor()
emailExist = cur.execute("""SELECT * FROM customerDetails""").fetchall()
print(emailExist)

My database file is in the same directory as this script.

AttributeError

I get this error:

>>> emailExist = cur.execute("""SELECT * FROM customerDetails""").fetchall()

>>> AttributeError: 'NoneType' object has no attribute 'fetchall'

Expected

When I run the same query in MySQLWorkbench the output is:

0 row(s) returned
1
  • 4
    execute doesn’t return anything. Call fetchall on the cursor object itself! Commented Jan 20, 2020 at 19:31

1 Answer 1

2

See docs MySQL :: MySQL Connector/Python Developer Guide :: 10.5.6 MySQLCursor.fetchall() Method.

Issue

The error-message

AttributeError: 'NoneType' object has no attribute 'fetchall'

for statement

cur.execute("""SELECT * FROM customerDetails""")

reads:

  • the execute-statement returned None
  • and the return value of None doesn't have fetchall attribute or method

Fix

Execute the cursor-methods in this order:

  1. cur.execute(sql) to create the cursor (in cur, not as return)
  2. cur.fetchall() to return the result
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.